how to find out number of rows in particular matrix of the cell?
Show older comments
Suppose i have 1*2 cell as shown below
A B
[1 2] [1 2 ; 3 4]
how can i find out number of rows in each cell..in these example they are 1 and rsply. for A and B.
Accepted Answer
More Answers (1)
Azzi Abdelmalek
on 15 Jul 2013
out=cellfun(@(x) size(x,1),a)
4 Comments
siddhesh rane
on 15 Jul 2013
Azzi, as I understand it, cellfun('size',a,1) is the preferable way to do this, as the anonymous function call is not optimized like the string arguments to cellfun.
>> tic;cellfun(@(x)size(x,1),a);toc
Elapsed time is 0.000631 seconds.
vs.
>> tic;cellfun('size',a,1);toc
Elapsed time is 0.000290 seconds.
Or, a more dramatic example:
>>a = repmat({rand(7)},800,1000);
>> tic;cellfun(@(x)size(x,1),a);toc
Elapsed time is 6.095846 seconds.
vs.
tic;cellfun('size',a,1);toc
Elapsed time is 0.011584 seconds.
Azzi Abdelmalek
on 15 Jul 2013
Sorry, my connection is very bad, I did not see your answer, I will remove mine
No worries, I often have the same problem. I just made that comment in order to clarify the difference between our answers (and to give you guys a chance to correct me in case I was mistaken :P). It wouldn't hurt to leave it up in order to have an example of how to use anonymous function handles with cellfun, especially since the list of available string arguments is pretty short and you oftentimes have to resort to function handles. I just wanted to clarify that, when available, the string arguments were best.
Categories
Find more on Cell Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!