find all elements (cells) of a single element (cell) in cell array

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

I don't understand, "The answer that I wish to have is (2) which refers to the index of these elements separately"
MA
MA 2019년 8월 28일
편집: MA 2019년 8월 28일
The second row of (c) contains {3} {2} which is actually the same as c{1,1} [3,2] but c{1,1} is combind together. So, 2 here refers to the second row.
I see. So you want to return the row index of C that, when horizontally concatented, equals the vector in C{1,1}.

댓글을 달려면 로그인하십시오.

 채택된 답변

MA
MA 2019년 8월 28일
I found the answer if anyone is looking for something similar
for i=2:size(c,1)
if (isequal(c{1,1},cell2mat(c(i,:))))
index=i;
end
end

댓글 수: 2

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)));
Thanks alot. It really helps

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Luna
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

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

MA
2019년 8월 28일

댓글:

MA
2019년 8월 28일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by