I have a cell array,
a=cell(2,1);
a{1,1}=[1 2 3];
a{2,1}=[4 5];
I need to calculate the sum of lengths of fields of a
, i.e. the answer should be 3+2=5
. This can be done using for
loop,
sum=0;
for i=1:size(a,1)
sum = sum + size(a{i},2);
end
But, I need one line command without loops. Any thoughts?
For a one-liner, use cellfun
sum(cellfun(@length,a))
cellfun
applies the command length
to each element of a
, then sum
adds the output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With