How can I replace value of specific cell value depending on another matrix?

4 ビュー (過去 30 日間)
A=[8 3 1; 0 0 0; 7 9 7; 5 5 5; 1 4 5]; D=[5 5 0; 7 9 0]; How can I compare between A and D depending on the first two columns and update the D matrix like,
D_modified=[5 5 5; 7 9 7];

採用された回答

Birdman
Birdman 2018 年 4 月 4 日
編集済み: Birdman 2018 年 4 月 4 日
D_modified=A(all(ismember(A(:,1:2),D(:,1:2)),2),:)

その他の回答 (1 件)

Akira Agata
Akira Agata 2018 年 4 月 4 日
If some rows in D does not have corresponding rows in A, need some more trick. Here is an example.
% Sample data (1st and 4th row of D has no corresponding row in A)
A = [8 3 1; 0 0 0; 7 9 7; 5 5 5; 1 4 5];
D = [5 4 0; 7 9 0; 8 3 0; 8 2 1];
[~,loc] = ismember(D(:,1:2),A(:,1:2),'rows');
idx = loc ~= 0;
D_modified(idx,:) = A(loc(idx),:);
  1 件のコメント
Mohammad Hossain
Mohammad Hossain 2018 年 4 月 4 日
I want to modify the D. replace 0 in both rows by corresponding value from A.

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

Community Treasure Hunt

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

Start Hunting!

Translated by