Find values and positions of a matrix referring to another matrix

18 ビュー (過去 30 日間)
Ricardo Gutierrez
Ricardo Gutierrez 2019 年 10 月 1 日
コメント済み: Ricardo Gutierrez 2019 年 10 月 1 日
Hi !!! Good day.
I hope you can help me
I have two matrices
A = [2 4 7];
B = [3 4 6
     1 5 7
     2 8 9
     9 2 4];
From the first element of matrix A find position and value in the first column of matrix B
From the second element of matrix A find position and value in the second column of matrix B
From the third element of matrix A find position and value in the third column of matrix B
In the end I will have two vectors;
one with the values 2, 4, 7 and the other with the positions 3, 1, 2
In reality, matrix A is 1 X 18 and matrix B has 720 X 18
Thank you very much for your help.
Regards.

採用された回答

David Hill
David Hill 2019 年 10 月 1 日
for i=1:length(A)
C(i)=find(B(:,i)==A(i),1);%will there always be only one match?
end

その他の回答 (1 件)

Stephen23
Stephen23 2019 年 10 月 1 日
編集済み: Stephen23 2019 年 10 月 1 日
This is MATLAB, so you should learn how to efficiently solve tasks like this without loops:
>> A = [2,4,7];
>> B = [3,4,6;1,5,7;2,8,9;9,2,4];
>> X = bsxfun(@eq,A,B); % or for >=R2016b simply X = A==B;
>> V = B(X)
V =
2
4
7
>> [R,~] = find(X)
R =
3
1
2

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by