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 element from matrix

Subject: Deleting element from matrix

From: stepan

Date: 5 Apr, 2011 17:07:05

Message: 1 of 5

Hello everyone,

Can someone help me please with this code?
I have vectors (coordinates) x = [1 1 2 3 5 6 1 1 5] and y = [2 3 6 8 4 1 1 1 5]
If I plot them, everything's ok, it works...

plot(x, y, 'bo') for example...

Now I would like to delete some elemets, to be accurate: If the x OR y is less than or equal to 2 I would like to delete this elemet and plot without these coordinates ... I hope you do understand :-)

x = [1 1 2 3 5 6 1 1 5];
y = [2 3 6 8 4 1 1 1 5];
numberOfElements = numel(x);

for i=1:1:numberOfElements
     if (x(i) > 3) || (y(i) > 3)
        x(i) = [];
        y(i) = [];
      end;
end;
plot(x,y,'bo');

That is my idea, but it doesn't work.

Please help if you can,
Thank you,

stepan89
        

Subject: Deleting element from matrix

From: Florin Neacsu

Date: 5 Apr, 2011 17:39:05

Message: 2 of 5

"stepan" wrote in message <infi7o$r7l$1@fred.mathworks.com>...
> Hello everyone,
>
> Can someone help me please with this code?
> I have vectors (coordinates) x = [1 1 2 3 5 6 1 1 5] and y = [2 3 6 8 4 1 1 1 5]
> If I plot them, everything's ok, it works...
>
> plot(x, y, 'bo') for example...
>
> Now I would like to delete some elemets, to be accurate: If the x OR y is less than or equal to 2 I would like to delete this elemet and plot without these coordinates ... I hope you do understand :-)
>
> x = [1 1 2 3 5 6 1 1 5];
> y = [2 3 6 8 4 1 1 1 5];
> numberOfElements = numel(x);
>
> for i=1:1:numberOfElements
> if (x(i) > 3) || (y(i) > 3)
> x(i) = [];
> y(i) = [];
> end;
> end;
> plot(x,y,'bo');
>
> That is my idea, but it doesn't work.
>
> Please help if you can,
> Thank you,
>
> stepan89
>


Hi,

The negation of OR is AND !
Try this

ind=x>=3.*ones(size(x)) & y>=3.*ones(size(y));
x_new=x(ind);
y_new=y(ind);

Regards,
Florin

Subject: Deleting element from matrix

From: Frank

Date: 5 Apr, 2011 17:39:13

Message: 3 of 5

>
> Now I would like to delete some elemets, to be accurate: If the x OR y is less than or equal to 2 I would like to delete this elemet and plot without these coordinates ... I hope you do understand :-)
>

One way....

plot(x(~(x<=2 | y<=2)),y(~(x<=2 | y<=2)),'bo')

Subject: Deleting element from matrix

From: Matt J

Date: 5 Apr, 2011 17:54:05

Message: 4 of 5

"stepan" wrote in message <infi7o$r7l$1@fred.mathworks.com>...
>
> Now I would like to delete some elemets, to be accurate: If the x OR y is less than or equal to 2 I would like to delete this elemet and plot without these coordinates ... I hope you do understand :-)
==============


ind=~(x<=2 | y<=2);

plot(x(ind),y(ind),'bo')

Subject: Deleting element from matrix

From: stepan

Date: 5 Apr, 2011 18:10:08

Message: 5 of 5

Frank <fbleahy@yahoo.com> wrote in message <610f841c-7407-44ab-aa92-4d15457855f9@w9g2000prg.googlegroups.com>...
> >
> > Now I would like to delete some elemets, to be accurate: If the x OR y is less than or equal to 2 I would like to delete this elemet and plot without these coordinates ... I hope you do understand :-)
> >
>
> One way....
>
> plot(x(~(x<=2 | y<=2)),y(~(x<=2 | y<=2)),'bo')

Thank you...

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