Giacomo Alessandroni
Giacomo Alessandroni

Reputation: 696

MATLAB Delete element in a matrix out of specific parameters

I have a matrix with two colons [x, y]. These are the data of a sampling. I have to remove the data out of range, all the data out of a rectangle from (x1, y1) to (x2, y2).

For me is not a problem remove the data in a vector, I use:

X = X(X > x1);
X = X(X < x2);
Y = Y(Y > y1);
Y = Y(Y < y2);

But this solution do not work, because do not removes all external value, i.e. I can not write X = X(Y > y1).

Now, I know that this problem can be solved with a simple loop for, but I think that in Matlab environment there are more than one solutions like mine (that works only with vector, not with a matrix).

Thankyou and bye, Giacomo

Upvotes: 0

Views: 71

Answers (2)

rayryeng
rayryeng

Reputation: 104565

Simply create a logical mask that encapsulates all of the solutions together, then index into your point array. Assuming that data stores your two columns of data, you can simply do:

X = data(:,1);
Y = data(:,2);
ind = X > x1 & X < x2 & Y > y1 & Y < y2;
points = data(ind,:);

Upvotes: 2

Giacomo Alessandroni
Giacomo Alessandroni

Reputation: 696

X = data(:,1);
Y = data(:,2);

mask = (X>x1 & X<x2).*(Y>y1 & Y<y2);
X = X.*mask;
Y = Y.*mask;
X(X==0)=[];
Y(Y==0)=[];

In this way I obtain only the data internal to my rectangle.

Upvotes: 0

Related Questions