Indexing of cell array using cellfun
10 次查看(过去 30 天)
显示 更早的评论
I have a cell array (10x1) where each cell is a numeric array (Mx2) and a vector with key indicies.
%Example [Code formatting SCd]
C=cell(10,1)
C{1}=rand(4,2)*10
C{2}=rand(5,2)*10...C{10}=rand(3,2)*10
V=1:10
I want to extract from C{1} the first element in V(1) C{2} the 2nd element of V(2)....
my initial thought was something like this, which of course doesn't work but...
out=cellfun(@(x) x(v(x),:),C, 'uniformoutput',false)
any suggestions on how do do this outside of a loop?
Thank You!
0 个评论
回答(3 个)
Sean de Wolski
2011-10-19
C = mat2cell(rand(100,2),ones(1,10)*10,2);
V = randperm(10).';
out = cellfun(@(c,idx)c(idx,:),C,num2cell(V))
perhaps?
0 个评论
Jason Schroder
2011-10-19
1 个评论
Sean de Wolski
2011-10-19
that's what it is now that I made the edit. Basically it takes two 10x1 cells and does the same thing to each row of cells:
so
C(1)then the V(1) or V(1,:) parts etc.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!