Thread Subject: removing zero elements

Subject: removing zero elements

From: M K

Date: 16 Oct, 2009 11:23:04

Message: 1 of 3

I have pair of arrays

x=[1,2,3,0,5,6,0];
y=[3,0,5,6,7,0,9];

(these are simplified examples)

I would like to remove corresponding elements from both arrays if either of them have a zero in it.

so in the above example, I am looking to have an answer of

x=[1,3,5];
y=[3,4,7];

How would I go about doing this?

any help would be most useful.

 

Subject: removing zero elements

From: Mirek L

Date: 16 Oct, 2009 11:48:02

Message: 2 of 3

"M K" <maha_k@mathworks.com> wrote in message <hb9l2n$a6s$1@fred.mathworks.com>...
> I have pair of arrays
>
> x=[1,2,3,0,5,6,0];
> y=[3,0,5,6,7,0,9];
>
> (these are simplified examples)
>
> I would like to remove corresponding elements from both arrays if either of them have a zero in it.
>
> so in the above example, I am looking to have an answer of
>
> x=[1,3,5];
> y=[3,4,7];
>
> How would I go about doing this?
>
> any help would be most useful.
>
>

x1 = x(x.*y ~= 0)
y1 = y(x.*y ~= 0)

Subject: removing zero elements

From: Loren Shure

Date: 16 Oct, 2009 12:10:21

Message: 3 of 3

In article <hb9l2n$a6s$1@fred.mathworks.com>, maha_k@mathworks.com
says...
> I have pair of arrays
>
> x=[1,2,3,0,5,6,0];
> y=[3,0,5,6,7,0,9];
>
> (these are simplified examples)
>
> I would like to remove corresponding elements from both arrays if either of them have a zero in it.
>
> so in the above example, I am looking to have an answer of
>
> x=[1,3,5];
> y=[3,4,7];
>
> How would I go about doing this?
>
> any help would be most useful.
>
>
>

anyzero = any([x;y] ==0)
x(anyzero) = []
y(anyzero) = []

I think you have a typo in expected results.

>> x=[1,2,3,0,5,6,0];
>> y=[3,0,5,6,7,0,9];
>> anyzero = any([x;y] ==0)
anyzero =
     0 1 0 1 0 1 1
>> x(anyzero) = []
x =
     1 3 5
>> y(anyzero) = []
y =
     3 5 7

--
Loren
http://blogs.mathworks.com/loren

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
remove pairs of... M K 16 Oct, 2009 07:24:05
rssFeed for this Thread

Contact us at files@mathworks.com