Matching elements in a matrix with elements in another matrix

19 ビュー (過去 30 日間)
SChow
SChow 2020 年 2 月 5 日
コメント済み: SChow 2020 年 2 月 6 日
I have a matrix,
A=[1 3 4
3 1 5
NaN 3 6]
and another index matrix
B=[1 333
2 356
3 112
4 789
5 762
6 221
7 876
8 922]
I want to match the elements in matrix A with the correesponding index (1st column) in matrix B and return
C=[333 112 789
112 333 762
NaN 112 221]
I tried using ismember, (by first making matrix A to a vector), however it did not work

採用された回答

Stephen23
Stephen23 2020 年 2 月 5 日
>> [idx,idy] = ismember(A,B(:,1));
>> C = nan(size(A));
>> C(idx) = B(idy(idx),2)
C =
333 112 789
112 333 762
NaN 112 221
  1 件のコメント
SChow
SChow 2020 年 2 月 6 日
Thanks a lot! It works! :) :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by