remove numbers of an matrix from another matrix

4 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 9 月 18 日
コメント済み: Alberto Acri 2023 年 9 月 18 日
Hi. How can I remove numbers of an matrix from another matrix?
I have two matrices:
M1 = [7 21; 41 52; 47 65; 14 41; 14 55];
M11 = [14 55; 47 65; 41 52; 14 41; 7 21];
M2 = [5 15; 7 21; 41 52; 47 65; 98 74; 14 41; 36 54; 36 47; 14 55; 14 88];
  • I need to check if M1 (or M11) is contained in M2.
  • I need to remove the values of M1 (or M11) from M2 with this end result:
M3 = [5 15; 98 74; 36 54; 36 47; 14 88];
  2 件のコメント
Harry
Harry 2023 年 9 月 18 日
To clarify, are you wanting to remove the rows of M2 if the entire row is replicated in either M1 or M11, or remove the row if only one number is repeated?
For example, if the you had:
M1 = [7 22; 41 52]
Would the [7 21] row need removing from M2 or not?
Alberto Acri
Alberto Acri 2023 年 9 月 18 日
Good observation. If the row of M1 (or M11) is present in M2!

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

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 18 日
編集済み: Dyuman Joshi 2023 年 9 月 18 日
M1 = [7 21; 41 52; 47 65; 14 41; 14 55];
M11 = [14 55; 47 65; 41 52; 14 41; 7 21];
M2 = [5 15; 7 21; 41 52; 47 65; 98 74; 14 41; 36 54; 36 47; 14 55; 14 88];
%Checking which rows in M2 are common with M1
[idx1,idx2] = ismember(M2,M1,'rows')
idx1 = 10×1 logical array
0 1 1 1 0 1 0 0 1 0
idx2 = 10×1
0 1 2 3 0 4 0 0 5 0
idx1 shows whether a row in M2 is present in M1 or not.
idx2 shows the corresponding indices of rows of M1 if present, 0 if not -
The 1st row in M2 is not common with in M1, The 2nd row in M2 is common with 1st row in M1, and so on.
%Removing the common rows
M2(idx1,:) = []
M2 = 5×2
5 15 98 74 36 54 36 47 14 88
%or
%Retaining the uncommon rows
%M2=M2(~idx1,:);

その他の回答 (1 件)

Steven Lord
Steven Lord 2023 年 9 月 18 日
Call setdiff with the 'rows' input.

カテゴリ

Help Center および File ExchangeFilter Design についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by