Find the corresponding matrix

조회 수: 1 (최근 30일)
Chenglin Li
Chenglin Li 2023년 5월 19일
댓글: Chenglin Li 2023년 5월 19일
My matrix A is going to be an n by 4 matrix, and B is going to be an m by 1 column matrix, and I want to find all the columns in A and if B exists, then I want to represent the row number of A and extract the data from that row of A
For example:
A =[14 17 19 2
19 12 11 20
20 12 11 7
18 5 16 6];
B = [1;2;5;6;11;12;14;17;19;20];
The result should be :
index = 1 2;
data = [[14 17 19 2
19 12 11 20];
I wrote a program to show empty, do not know where the error
row_numbers = [];
for row=1:size(A, 1)
if all(ismember(B, A(row, :)))
row_numbers = [row_numbers row];
end
end
disp(row_numbers);

채택된 답변

Askic V
Askic V 2023년 5월 19일
편집: Askic V 2023년 5월 19일
Is this what you need?
A =[14 17 19 2
19 12 11 20
20 12 11 7
18 5 16 6];
B = [1;2;4;5;6;11;12;14;17;19;20];
rA = size(A,1);
ind = ismember(B,1:rA);
M = A(B(ind),:)
M = 3×4
14 17 19 2 19 12 11 20 18 5 16 6
  댓글 수: 1
Chenglin Li
Chenglin Li 2023년 5월 19일
Yes, that's what I want. Thank you very much!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by