Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to remove corresponding points from two matricies

Subject: How to remove corresponding points from two matricies

From: Garrett Pataky

Date: 17 Nov, 2010 18:24:05

Message: 1 of 4

I have a matrix X and a matrix Y both with 100 points. Matrix X has negative values that I want to remove. Lets say I have 20 negative points, so I can remove them, but now I am left with X having 80 points and Y having 100 points. I need these two matricies to be the same size.

How can I remove the points corresponding to the points I removed from X from the Y matrix as well? Thanks.

Subject: How to remove corresponding points from two matricies

From: Steven_Lord

Date: 17 Nov, 2010 18:35:37

Message: 2 of 4



"Garrett Pataky" <pataky.removethis.2@illinois.edu> wrote in message
news:ic16k5$rm1$1@fred.mathworks.com...
> I have a matrix X and a matrix Y both with 100 points. Matrix X has
> negative values that I want to remove. Lets say I have 20 negative
> points, so I can remove them, but now I am left with X having 80 points
> and Y having 100 points. I need these two matricies to be the same size.
>
> How can I remove the points corresponding to the points I removed from X
> from the Y matrix as well? Thanks.

Logical indexing with either a vector of points to remove or a vector of
points to keep.

% Sample data
X = randn(1, 100);
Y = rand(1, 100);
% Operate on a copy so you can compare
X2 = X;
Y2 = Y;

% Remove points
pointsToRemove = X < 0;
X2(pointsToRemove) = [];
Y2(pointsToRemove) = [];

% Keep points, operating on a copy
X2 = X;
Y2 = Y;
pointsToKeep = X >= 0;
X2 = X2(pointsToKeep);
Y2 = Y2(pointsToKeep);

These are not _exactly_ the same if X or Y contain NaN values, but you will
need to decide how to handle that situation if or when it arises.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to remove corresponding points from two matricies

From: Sean de

Date: 17 Nov, 2010 18:37:04

Message: 3 of 4

"Garrett Pataky" <pataky.removethis.2@illinois.edu> wrote in message <ic16k5$rm1$1@fred.mathworks.com>...
> I have a matrix X and a matrix Y both with 100 points. Matrix X has negative values that I want to remove. Lets say I have 20 negative points, so I can remove them, but now I am left with X having 80 points and Y having 100 points. I need these two matricies to be the same size.
>
> How can I remove the points corresponding to the points I removed from X from the Y matrix as well? Thanks.

So get rid of the ones in Y first:
Y(X<0) = [];
X(X<0) = [];

Subject: How to remove corresponding points from two matricies

From: Garrett P

Date: 17 Nov, 2010 18:56:05

Message: 4 of 4

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ic17cg$igr$1@fred.mathworks.com>...
> "Garrett Pataky" <pataky.removethis.2@illinois.edu> wrote in message <ic16k5$rm1$1@fred.mathworks.com>...
> > I have a matrix X and a matrix Y both with 100 points. Matrix X has negative values that I want to remove. Lets say I have 20 negative points, so I can remove them, but now I am left with X having 80 points and Y having 100 points. I need these two matricies to be the same size.
> >
> > How can I remove the points corresponding to the points I removed from X from the Y matrix as well? Thanks.
>
> So get rid of the ones in Y first:
> Y(X<0) = [];
> X(X<0) = [];

Thanks that worked great.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us