Find equal rows between cell array and matrix into for loop

조회 수: 1 (최근 30일)
Riccardo Rossi
Riccardo Rossi 2019년 7월 18일
댓글: Bruno Luong 2019년 7월 18일
Hi everybody,
i have a matrix (A) and a cell array (B) as follow:
A
-0.11 7.17 3.66
-0.09 3.45 1.55
-0.21 2.17 9.87
-0.14 4.88 6.66
B{1,1} B{1,2}
-0.09 3.45 1.55 -0.14 4.88 6.66
I need to detect the number of row of A equal to B{1,1} and B{1,2}. In this exemple i need to create a matrix or a cell array C as follow:
C
2
4
Thank you very much!
Riccardo

채택된 답변

Bruno Luong
Bruno Luong 2019년 7월 18일
편집: Bruno Luong 2019년 7월 18일
Try this:
A=[-0.11 7.17 3.66
-0.09 3.45 1.55
-0.21 2.17 9.87
-0.14 4.88 6.66]
B{1,1} = [-0.09 3.45 1.55]
B{1,2} = [-0.14 4.88 6.66]
C = find(ismember(A,cat(1,B{:}),'row'))
  댓글 수: 2
Riccardo Rossi
Riccardo Rossi 2019년 7월 18일
Thank you for your answer. It runs!
Do you know if it is possible to obtain the following result?:
C
2 1
4 2
with the second column that represent the correspondent cell of B (respectively B{1,1} B{1,2}).
Thanks
Bruno Luong
Bruno Luong 2019년 7월 18일
bnum = repelem((1:numel(B))',cellfun('size',B(:),1))
[tf,bloc] = ismember(A,cat(1,B{:}),'row');
C = [find(tf),bnum(bloc(tf))]

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

추가 답변 (1개)

Alex Mcaulley
Alex Mcaulley 2019년 7월 18일
A = [
-0.11 7.17 3.66
-0.09 3.45 1.55
-0.21 2.17 9.87
-0.14 4.88 6.66];
B = {[-0.09 3.45 1.55],[-0.14 4.88 6.66]};
[~,C] = ismember(cell2mat(B'),A,'rows')

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by