Tak
Tak

Reputation: 3616

Delete row in a matrix Matlab

I have two matrices A "only has one column" and B which has more than one colomn but the same number of rows as A as shown below, what I want to do is to check the rows in A which has elements equal to -1 and remove delete this row from both matrices A and B, so in the example below I want to delete row index 3 and 6 from both matrices A and B, any advise?

A=  6
    3
    -1
    6
    6
    -1
    2
    4

and B= -0.511774504646677   0.435674206557952   1.07400000000000
-0.509871997194459  0.437576714010170   1.07400000000000
-0.507969489742241  0.439479221462388   1.07400000000000
-0.506586007364545  0.429374013677012   1.07100000000000
-0.504201297562686  0.439754515167456   1.07100000000000
-0.501883219358233  0.428847974750132   1.07100000000000
-0.501415044713309  0.431930562861652   1.07100000000000
-0.499537085744345  0.433808521830616   1.07100000000000

Upvotes: 0

Views: 325

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Nearly the same as was proposed already, but without calculating the same index twice. Also this will not fail if you change the order of changing A and B.

idx = A~=-1;
A = A(idx);
B = B(idx);

Upvotes: 2

Marc Claesen
Marc Claesen

Reputation: 17026

Use logical indexing based on A:

B=B(A~=-1,:);
A=A(A~=-1);

Upvotes: 6

Related Questions