Replacing same rows with same variables in first to third column

Hi. I have two matrices named aa and m.
aa=[1 2 3 7
4 5 6 8
7 8 9 9
3 2 3 8
6 5 6 7
7 8 7 9];
mm=[1 2 3 5
4 5 6 9
7 8 9 6];
I want to replace the rows of aa with the rows of m that have the same values in first to third column.
How can I do that?
Thanks a lot.

2 件のコメント

Image Analyst
Image Analyst 2017 年 12 月 21 日
What if m is
mm=[1 2 3 5
1 2 3 9
4 5 6 9
7 8 9 6];
Which row of m, the first or the second, should replace the first row of aa?
mr mo
mr mo 2017 年 12 月 21 日
In my case there are only one same rows in ‘aa’ and ‘mm’

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

回答 (2 件)

Jos (10584)
Jos (10584) 2017 年 12 月 21 日

1 投票

No need for loops at all! Use both outputs of ismember and replace accordingly
aa = [1 2 3 7
4 5 6 8
7 8 9 9
3 2 3 8
6 5 6 7
7 8 7 9
1 2 3 999];
mm = [1 2 3 5
4 5 6 9
7 8 9 6];
[tf,loc] = ismember(aa(:,1:3),mm(:,1:3),'rows')
% tf indicates which rows in aa are found where (loc) in mm (only for columns 1-3)
aa(tf,:) = mm(loc(tf),:) % replace those rows
KSSV
KSSV 2017 年 12 月 21 日
編集済み: KSSV 2017 年 12 月 21 日

0 投票

aa(1:3,1:3) = mm(1:3,1:3) ;

1 件のコメント

mr mo
mr mo 2017 年 12 月 21 日
Thanks but this codes lost the fourth column values of mm.

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

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2017 年 12 月 21 日

コメント済み:

2017 年 12 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by