How to count the amount of cells?
115 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello kity
el 21 de Dic. de 2012
Hi
I have a cell array, 1x 90. eacht of them has diff set of data,
for example, 1x1 has 10 cells , 1x2 5 cell etcs.
i want to count to count the amount of cells
numel(cellarrayname) gives 90... thats not what i want.
it should count every cell array add cellarray 2 +cell aray 3 etc..
0 comentarios
Respuesta aceptada
José-Luis
el 21 de Dic. de 2012
your_count = cellfun(@(x) numel(x),your_cell_array);
4 comentarios
Ni Putu Dewi Nurmalasari
el 29 de Nov. de 2016
i have the similar problem. I have data 1x1000 struct with 1 field , row1=1x61 double, row 2=2x50 double, row 3=3x31double, row4=[5,4], row5=[50,55,53]...row 1000=2. I want to make a plot of how many counts for nx61, nx50.....nx2,nx1 in this case if every row is nxm which m value(1....61), i will put 1x61,2x62,3x61..nx61 into 1 group of data. I am hoping to get 61 data in total.thanks
Más respuestas (2)
Wayne King
el 21 de Dic. de 2012
Editada: Wayne King
el 21 de Dic. de 2012
You want to count how many cells are in each element of the cell array?
x = cell(3,1);
x{1} = cell(2,1); x{2} = cell(25,1); x{3} = cell(30,1);
out = cellfun(@numel,x,'uni',0);
numberofelements = sum(cell2mat(out));
In the above, cellfun() gives you cell array with each cell containing the number of elements in each cell of your original cell array.
cell2mat() turns that cell array into a vector, so now you have a vector where each elements gives the number of each elements in the cell arrays of your original cell array.
Finally, I just sum the elements of the vector to get the total number of elements.
0 comentarios
Jan
el 29 de Nov. de 2016
Editada: Jan
el 29 de Nov. de 2016
cellfun(@numel) is slower than the hard coded
out = cellfun('prodofsize', x);
Unfortuantely this efficient method is hidden in the "Backward compatibility" link. Using the function handle requires a call from Mex to Matlab for each element, while the string method is processed directly inside the C-Mex function. Former versions of Matlab contained the cellfun.c file, which revealed this.
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!