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:
deleting elements from vector

Subject: deleting elements from vector

From: Shobana

Date: 25 Dec, 2012 07:07:07

Message: 1 of 4

hai friends...
   I need to delete some values in the vector based on the values in the another vector
for example:
  e = [1 1 0 0 2 3 , . . . . n]
  a = [1 2 3 4 5 6 ...........n]
the result should be like this b = [1 2 5 6.....n] the value of 'a' corresponding to the place where e = = 0 should be removed.I tried like this
for j =1:length(e)
  if(e == 0)
   data[j] = [];
end
end

but it gives wrong answer like [1 2 4 6..n] .here the length of 'a' and 'e' are same.please reply me as soon as possible
 

Subject: deleting elements from vector

From: Nasser M. Abbasi

Date: 25 Dec, 2012 07:14:08

Message: 2 of 4

On 12/25/2012 1:07 AM, Shobana wrote:
> hai friends...
> I need to delete some values in the vector based on the values in the another vector
> for example:
> e = [1 1 0 0 2 3 , . . . . n]
> a = [1 2 3 4 5 6 ...........n]
> the result should be like this b = [1 2 5 6.....n] the value of 'a' corresponding
>to the place where e = = 0 should be removed.I tried like this
> for j =1:length(e)
> if(e == 0)
> data[j] = [];
> end
> end
>
> but it gives wrong answer like [1 2 4 6..n] .here the length of 'a' and 'e' are same.
>please reply me as soon as possible
>
>

EDU>> e = [1 1 0 0 2 3]
EDU>> a = [1 2 3 4 5 6]
EDU>> a(e==0)=[]

      1 2 5 6

--Nasser

Subject: deleting elements from vector

From: Roger Stafford

Date: 25 Dec, 2012 07:29:07

Message: 3 of 4

"Shobana " <manoharanshobana@ymail.com> wrote in message <kbbjar$5g6$1@newscl01ah.mathworks.com>...
> for j =1:length(e)
> if(e == 0)
> data[j] = [];
> end
> end
- - - - - - - - - -
  To make that for-loop work you would have to scan backwards through the data array:
 
 n = length(e);
 for j = n:-1:1

  However, Nasser's way is better:

 a(e==0) = [];

Roger Stafford

Subject: deleting elements from vector

From: Shobana

Date: 26 Dec, 2012 04:42:10

Message: 4 of 4

"Roger Stafford" wrote in message <kbbkk3$98l$1@newscl01ah.mathworks.com>...
> "Shobana " <manoharanshobana@ymail.com> wrote in message <kbbjar$5g6$1@newscl01ah.mathworks.com>...
> > for j =1:length(e)
> > if(e == 0)
> > data[j] = [];
> > end
> > end
> - - - - - - - - - -
> To make that for-loop work you would have to scan backwards through the data array:
>
> n = length(e);
> for j = n:-1:1
>
> However, Nasser's way is better:
>
> a(e==0) = [];
>
> Roger Stafford

thanks a lot

Tags for this Thread

No tags are associated with 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