フィルターのクリア

Need to match Data

2 ビュー (過去 30 日間)
Johannes
Johannes 2012 年 10 月 8 日
Hello,
I like to match the entries in column 2 of matrix A to the reference numbers of B. I'm going to do that for large data sets. Code like follows does exactly what I want but will be to slow. Is there an intelligent function/way available to do that? Thanks a lot!
A = [2 3 5 7; 23 54 76 86]';
B = [1:7]';
j=1;
for i = 1:7
if B(i,1) == A(j,1)
B(i,2) = A(j,2);
j = j+1;
end
end
result: B =
1 0
2 23
3 54
4 0
5 76
6 0
7 86

採用された回答

Matt Fig
Matt Fig 2012 年 10 月 8 日
B(A(:,1),2) = A(:,2)
  2 件のコメント
Matt Fig
Matt Fig 2012 年 10 月 8 日
Johannes comments:
"Thank you. That was a bad example, my mistake.
What would be the solution for vectors like that:
A = [7 3 5 9; 23 54 76 86]';
B = [3:15]';"
Matt Fig
Matt Fig 2012 年 10 月 8 日
編集済み: Matt Fig 2012 年 10 月 8 日
In that case:
A = [7 3 5 9; 23 54 76 86]';
B = [3:15]';
As = sortrows(A,1);
idx = ismember(B,As(:,1));
B(idx,2) = As(:,2)

サインインしてコメントする。

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 10 月 8 日
編集済み: Andrei Bobrov 2012 年 10 月 8 日
A = [7 3 5 9; 23 54 76 86]';
B = [3:15]';
[a,b] = ismember(B,A(:,1));
B(a,2) = A(b(a),2);

Johannes
Johannes 2012 年 10 月 9 日
thank you all! My final solution is the following:
B(ismember(A),2)=A(ismember(A,B),2);
Works perfect and very fast.
Thank you again for your help.
Best, Johannes

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by