find all elements (cells) of a single element (cell) in cell array
2 ビュー (過去 30 日間)
古いコメントを表示
I have the following cell array (c) which is 3*2. Now I want to find all elements of lets say c{1,1} which is [3,2]. The answer that I wish to have is (2) which refers to the index of these elements separately. Any possible ideas? thanks
c=
{[3,2]} {[4,1]}
{3} {2}
{4} {1}
3 件のコメント
Adam Danz
2019 年 8 月 28 日
I see. So you want to return the row index of C that, when horizontally concatented, equals the vector in C{1,1}.
採用された回答
MA
2019 年 8 月 28 日
2 件のコメント
Adam Danz
2019 年 8 月 28 日
If the idea is to return the row index of c that matches c{1,1}, here is an alternative.
% This line returns the logical index
index = cellfun(@(x)isequal([x{:}],c{1,1}),mat2cell(c,ones(size(c,1),1),2));
% This line returns the linear index (matching your for-loop)
index = find(cellfun(@(x)isequal([x{:}],c{1,1}),mat2cell(c,ones(size(c,1),1),2)));
その他の回答 (1 件)
Luna
2019 年 8 月 28 日
Maybe you can try this piece of code:
cellSizes = cellfun('prodofsize',c);
elementIndices = [];
elementValues = [];
for i = 1:numel(cellSizes)
for j = 1:cellSizes(i)
tempVar = c{i}(j);
elementValues = [elementValues,tempVar];
elementIndices = [elementIndices,find(c{i} == tempVar)];
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Array Geometries and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!