Search code examples
matlabimage-processingcell-array

calculating the number of columns in a row of a cell array in matlab


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


Solution

  • 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);