How to erase data from a matrix from the command line?

INTRO: Hi, I have a matrix Mij and would like to erase the rows i whenever the row-value of two columns are identical (for example:Mi2=Mi3)
Example:
M=3 1.2 13 2
1.2 0.3 0.3 10
10 2 1 0
0.7 5 3 0.4
The new matrix
Nij= 3 1.2 13 2
10 2 1 0
0.7 5 3 0.4
I wonder if someone could help me how to solve this issue
Thanks in advance
Emerson

 採用された回答

Matt Fig
Matt Fig 2012 年 10 月 26 日

1 投票

M=[3 1.2 13 2
1.2 0.3 0.3 10
10 2 1 0
0.7 5 3 0.4];
Nij = M(all(diff(sort(M,2),[],2),2),:)

5 件のコメント

Emerson De Souza
Emerson De Souza 2012 年 10 月 26 日
編集済み: Emerson De Souza 2012 年 10 月 26 日
Hi Matt Fig,
thank you for your suggestion. You answered exact to the situation: 'whenever the row-value of two columns are identical'. Now, how the command changes if i want to specify two columns, for example: columns 3 and 4? The matrix above will not have any row to erase, but I would like to know how to change the commands.
Thank you in advance for everything
Emerson
Matt Fig
Matt Fig 2012 年 10 月 26 日
You mean you want to delete columns 3 and 4? Or you only want to keep columns 3 and 4?
Emerson De Souza
Emerson De Souza 2012 年 10 月 26 日
編集済み: Emerson De Souza 2012 年 10 月 26 日
I mean if the row-values of columns 3 and 4 are identical, then erase that row (Not identical row values between columns 2 and 3 as above).
For example, if
M=[3 1.2 13 2
1.2 0.3 0.3 10
10 2 1 1
0.7 5 3 0.4];
then
N=[3 1.2 13 2
1.2 0.3 0.3 10
0.7 5 3 0.4];
Matt Fig
Matt Fig 2012 年 10 月 26 日
Same idea:
M = [1 2 3 4;5 6 7 8;1 2 3 3;6 7 8 9;3 4 5 5];
M(~diff(M(:,3:4),[],2),:) = []
Emerson De Souza
Emerson De Souza 2012 年 10 月 26 日
Now I got it, thank you for your attention
Wish you a nice night
Emerson

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by