0

i've got a cell array full of numbers, with 44 rows and different column length in each row
how could i calculate the number of columns in each row?(the columns which their contents are not empty)
i've used 2 different ways which both of them where wrong
the 1st one:

%a is the cell array
s=length(a)

it gives 44 which is the number of rows

the 2nd one

[row, columms]=size(a)

but it doesn't work either cause the number of columns is different in each row.
at least i mean the number of columns which are not empty
for example i need the number of columns in row one which it is 43(a{1 1:43}) but it gives the number of columns for each elements like a{1,1} which is 384 or a{1,2},a{1,3} and so on

samdean
  • 113
  • 2
  • 9

2 Answers2

1

You need to access each member of the cell array separately, you are looking for the size of the data contained in the cell - the cell is the container. Two methods

for loop:

cell_content_lengths=zeros(1,length(a));
for v=1:length(a)
    cell_content_lengths(v)=length(a{v});
end

cellfun:

cell_content_lengths=cellfun(@length,a);

Any empty cells will just have length 0. To extend the for-loop to matrices is trivial, and you can extend the cellfun part to cells containing matrix by using something like this, if you are interested:

cell_content_sizes=cell2mat(cellfun(@length,a,'uniformoutput',false));

(Note for the above, each element of a needs to have the same dimension, otherwise it will give errors about concatenating different size matrices)

EDIT

Based on your comment I think I understand what you are looking for:

non_empty_cols = sum(~cellfun(@isempty,a),2);

With thanks to @MZimmerman6 who understood it before me.

Hugh Nolan
  • 2,508
  • 13
  • 15
  • You would have to do sum the results of cellfun, especially each cell contains multiple other cells, so it can simply be `sum(cellfun(@length,a),2)` – MZimmerman6 Jul 22 '13 at 13:54
  • You don't have to sum them if I understood the original post - @samdean is looking for the size of each individual cell element's data. – Hugh Nolan Jul 22 '13 at 13:57
  • The only reason I assumed that this was not true was because he made mention of empty content cells which would happen if it was a pure 2-dimensional cell array. – MZimmerman6 Jul 22 '13 at 14:01
  • it does not give the correct answer. i try this code for a chosen example which i knew the number of columns is 43 for each row but it gives me the number of columns of each element in a row which is 384. for example i need the number of columns in row one which it is 43(a{1 1:43}) but it gives the number of columns for each elements like a{1,1} which is 384 or a{1,2},a{1,3} and so on which i do not really need it(thanks) – samdean Jul 22 '13 at 14:08
  • thank both of you the last code worked. i can't appreciate well enough while i don't have reputation to vote you,sorry – samdean Jul 22 '13 at 14:21
  • 1
    @HughNolan By the way, check out `cellfun` with a string function, _i.e_: `cellfun('length', ...)` and `cellfun('isempty', ...)`. Surprisingly they are [_much_ faster](http://undocumentedmatlab.com/blog/cellfun-undocumented-performance-boost/)... – Eitan T Jul 22 '13 at 15:19
  • Oh excellent, I had come across once this but I thought for some reason they had updated this. A quick check shows that they haven't (2012b): 100000 uses of `@length` takes 8.66 seconds, while 100000 uses of `'length'` takes 0.65 seconds. – Hugh Nolan Jul 22 '13 at 17:02
  • it gives the correct answer but after running the program 3 times, there would be this error:'Index exceeds matrix dimensions.' and the error wont go unless i shut down the matlab and run it again. what is the problem? – samdean Jul 22 '13 at 20:43
  • The problem is something else in the program. Since you haven't shown us the rest, I have no idea. – Hugh Nolan Jul 22 '13 at 21:37
  • @Hugh,thanks,i misused a varient, i correct that and the problem solved. thanks for you help – samdean Jul 23 '13 at 05:23
0

So what you're really asking, is "How many non-empty elements are in each row of my cell array?"

filledCells = ~cellfun(@isempty,a);
columns = sum(filledCells,2);
David K
  • 1,296
  • 18
  • 39
  • FYI, `cellfun('isempty', a)` is a lot faster (please see [a similar comment of mine](http://stackoverflow.com/questions/17789220/calculating-the-number-of-columns-in-a-row-of-a-cell-array-in-matlab#comment25953596_17789328) to Hugh's answer). – Eitan T Jul 22 '13 at 15:20
  • it gives the correct answer but after running the program 3 times, there would be this error:'Index exceeds matrix dimensions.' and the error wont go unless i shut down the matlab and run it again. what is the problem? – samdean Jul 22 '13 at 20:42
  • @samdean - It's difficult to know without some more information. What is changing during those three runs? Which line specifically causes the error? – David K Jul 22 '13 at 21:03
  • @David. nothing change actully. the code is in a separate part above all other and it wont change – samdean Jul 22 '13 at 21:15
  • thanks @David, you were right a misused variant caused the error, the problem solved by changing it – samdean Jul 23 '13 at 05:25