Deleting specific colums in a matrix

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
Star Strider am 11 Jul. 2017
Bearbeitet: Star Strider am 11 Jul. 2017

1 Stimme

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

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.

Melden Sie sich an, um zu kommentieren.

Hans Bauer
Hans Bauer am 11 Jul. 2017

0 Stimmen

Great. That works. Thanks a lot. By the way I`ve got another question: How does it works, when I have 3 conditions: I want to delete all colums when the number in row1 is >=3 and the number in row1 is <= 2 and when there is a 5 in row3. I tried this, but it doesn`t work.
LV = (M(:,1)>=3) & (M(:,1)<=2) & (M(:,3)==5);
M(LV,:) = [];

2 Kommentare

Akira Agata
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
Star Strider am 16 Jul. 2017
I addressed that in my Comment to my Answer, rather than a Comment here.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 11 Jul. 2017

Beantwortet:

am 16 Jul. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by