Get n-th value from a double array in a cell array
显示 更早的评论
Hi all,
i've a 200*200 cell array (test) with a n*1 double array in each, some cells are empty as well. I want to get the 5th value of the double (5,1).
1) Generate Index with only non-empty elements:
fct = @(d) find(d > 0, 1, 'first');
IndUpdates = cellfun(fct, test, 'Uni', 0);
2) Get size of Double Arrays
fct2 = @(idx, d) size(d,1);
IndSize = cellfun(fct2, IndUpdates, test, 'Uni', 0);
--> what is the 'idx' for? it does not run without it
3) Generate Index for cells with double arrays bigger than 5 elements
fct3 = @(d) find(d > MinUpdates, 1, 'first');
idxsus = cellfun(fct3, IndUpdates, test, 'Uni', 0);
until here it works fine. now i want the 5th value from every double array indexed by "idxsus". I thougt it would work like below, but it doesn't. (Error: Too many input arguments)
fct4 = @(d) d(MinUpdates,1);
result = cellfun(fct4, idxsus, test, 'Uni', 0);
I'm no matlab expert (as you can see) - is there anyone, who can fix my code with a little explanation?
Thank you in advance!
Florian
采纳的回答
更多回答(1 个)
Walter Roberson
2018-7-22
MinUpdates = 5;
lengths = cellfun(@length, test);
idxsus = find(lengths >= MinUpdates);
result = cellfun(@(d) d(MinUpdates), test(idxsus));
I would not typically use find() for this, but it is useful if you need the linear indices of which cells you are pulling the information out of.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!