Deleting specific colums in a matrix
Ältere Kommentare anzeigen
Hello, I´ve got a Matrix M:
1 3 5
2 9 7
4 7 0
3 8 5
3 9 2
How can I delete all colums with a 3 in row1 and a 5 in row3. I`ve tried it like this, but it doesn`t work.
cond1 = M(:,3)=5;
cond2 = M(:,1)=3;
condall = cond1 & cond2
M(condall,:) = [];
Antworten (3)
Star Strider
am 11 Jul. 2017
Bearbeitet: Star Strider
am 11 Jul. 2017
You need to do the tests in the same statement, because the conditions have to occur simultaneously:
Example —
M = [1 3 5
2 9 7
4 7 0
3 8 5
3 9 2];
LV = (M(:,1)==3) & (M(:,3)==5); % Logical Vector
M(LV,:) = []
M =
1 3 5
2 9 7
4 7 0
3 9 2
EDIT —
It is also necessary to use the ‘double equal’ ( == ) to test for equality, not a single equal. The ‘single equal’ is an assignment, the ‘double equal’ a test for equality.
1 Kommentar
Star Strider
am 11 Jul. 2017
The problem is that no value in column 1 (or in any other situation) can simultaneously be less than or equal to 2 and greater than or equal to 3.
Example —
M = (1:10)';
Q1 = (M(:,1)>=3) & (M(:,1)<=2)
Q1 =
10×1 logical array
0
0
0
0
0
0
0
0
0
0
If you reverse the conditions, you will get a valid logical vector:
Q2 = (M(:,1)>=2) & (M(:,1)<=3)
Q2 =
10×1 logical array
0
1
1
0
0
0
0
0
0
0
So, with your matrix ‘M’:
M = [1 3 5
2 9 7
4 7 0
3 8 5
3 9 2];
LV = (M(:,1)>=2) & (M(:,1)<=3) & (M(:,3)==5); % Logical Vector
M(LV,:) = []
M =
1 3 5
2 9 7
4 7 0
3 9 2
This is the same as the previous result.
Hans Bauer
am 11 Jul. 2017
2 Kommentare
Akira Agata
am 16 Jul. 2017
"the number in row1 is >=3 and the number in row1 is <= 2" ? That should be always false. Is this an editorial mistake ?
Star Strider
am 16 Jul. 2017
I addressed that in my Comment to my Answer, rather than a Comment here.
Andrei Bobrov
am 16 Jul. 2017
out = M(~ismember(M(:,[1,3]),[3,5]),:);
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!