Got Questions? Get Answers.
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:
Find row

Subject: Find row

From: Brendan

Date: 7 Apr, 2010 16:15:28

Message: 1 of 8

Hi,
I have a 1110x4 array and want to find the row in which a certain condition is satisfied..
For example if I have:
Y=[y1;y2;y3;y4] where y1,y2,y3,y4 are 1110 long vectors,
I want to find the row in which:
atan2(y1/y2)==certain value.

I can find the row for Max and Min using [C,I]=min(Y) etc but am not sure how to check if the atan2 condition is satisfied.
Any ideas? Im sure its relatively easy but...
Thanks
Brendan

Subject: Find row

From: Matt Fig

Date: 7 Apr, 2010 16:27:05

Message: 2 of 8

"Brendan " <c_uboid88@hotmail.co.uk> wrote in message <hpib30$9s4$1@fred.mathworks.com>...
> Hi,
> I have a 1110x4 array and want to find the row in which a certain condition is satisfied..
> For example if I have:
> Y=[y1;y2;y3;y4] where y1,y2,y3,y4 are 1110 long vectors,
> I want to find the row in which:
> atan2(y1/y2)==certain value.
>
> I can find the row for Max and Min using [C,I]=min(Y) etc but am not sure how to check if the atan2 condition is satisfied.
> Any ideas? Im sure its relatively easy but...
> Thanks
> Brendan


Some things are not right about your post. First of all, if y1-4 are 1110 element vectors, then Y CANNOT be 11104-by-4 as given. Second, atan2 takes 2 arguments. I think you should clarify these things.

Subject: Find row

From: Brendan

Date: 7 Apr, 2010 16:37:25

Message: 3 of 8

Sorry, I just meant that the columns of Y contain 1110 elements. i.e It is an array with 4 colums and 1110 rows. Does that not make sense?
And the atan2 is used because I need to find the row at which atan of y2/y1 equals a certain angle. y1 and y2 are the x and y co-ordinates of a particle respectively. So yes, I know it needs two arguments, those arguments are the values of y1 and y2 at each row! So I need the code to go through each row and check when atan2(y2/y1)==value and then tell me which row of Y this occurs in.
I hope this clarifies?
Brendan

Subject: Find row

From: us

Date: 7 Apr, 2010 16:45:10

Message: 4 of 8

"Brendan " <c_uboid88@hotmail.co.uk> wrote in message <hpicc5$64$1@fred.mathworks.com>...
> Sorry, I just meant that the columns of Y contain 1110 elements. i.e It is an array with 4 colums and 1110 rows. Does that not make sense?
> And the atan2 is used because I need to find the row at which atan of y2/y1 equals a certain angle. y1 and y2 are the x and y co-ordinates of a particle respectively. So yes, I know it needs two arguments, those arguments are the values of y1 and y2 at each row! So I need the code to go through each row and check when atan2(y2/y1)==value and then tell me which row of Y this occurs in.
> I hope this clarifies?
> Brendan

a small example of ML code would be worth so much more than this verbose description...

us

Subject: Find row

From: Brendan

Date: 7 Apr, 2010 17:00:24

Message: 5 of 8

y0 = [1;0;0;1];
t0=0
tend=.1
step=tend/0.01

for i=1:100
[t,y]=ode45(@function,[0:0.01:tend],y0,options);
x(1:length(y))=y(:,1);
y(1:length(y))=y(:,2);
xdot(1:length(y))=y(:,3);
ydot(1:length(y))=y(:,4);

y0=[y(step,1);y(step,2);y(step,3);y(step,4)]; % sets the last values calculated to
                                                               % new initial conditiosn
end
Y=[x,y,xdot,ydot]

so I want to find the row of Y at which atan2(y/x)==somevalue


Thanks again

Subject: Find row

From: Roger Stafford

Date: 7 Apr, 2010 17:03:05

Message: 6 of 8

"Brendan " <c_uboid88@hotmail.co.uk> wrote in message <hpib30$9s4$1@fred.mathworks.com>...
> Hi,
> I have a 1110x4 array and want to find the row in which a certain condition is satisfied..
> For example if I have:
> Y=[y1;y2;y3;y4] where y1,y2,y3,y4 are 1110 long vectors,
> I want to find the row in which:
> atan2(y1/y2)==certain value.
>
> I can find the row for Max and Min using [C,I]=min(Y) etc but am not sure how to check if the atan2 condition is satisfied.
> Any ideas? Im sure its relatively easy but...
> Thanks
> Brendan

  "Find" is the right word to use. With your atan2 example you could use:

 row = find(atan2(Y(:,1),Y(:,2))==value);

However, since atan2 values are subject to round off errors, this would probably not be what you want. It is too restrictive. You should rather have something like this:

 row = find(abs(atan2(Y(:,1),Y(:,2))-value)<tol);

where 'tol' is some suitably small value that would safely allow for all such rounding errors.

Roger Stafford

Subject: Find row

From: Matt Fig

Date: 7 Apr, 2010 17:08:08

Message: 7 of 8

find(atan2(Y(:,2),Y(:,1))==somevalue)

Note that you are likely to encounter floating point problems with such an equality comparison. You might want to do:

find(abs(atan2(Y(:,2),Y(:,1))-somevalue)<tol) % For some tolerance.

Subject: Find row

From: Brendan

Date: 7 Apr, 2010 17:35:15

Message: 8 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <hpie5o$q7g$1@fred.mathworks.com>...
> find(atan2(Y(:,2),Y(:,1))==somevalue)
>
> Note that you are likely to encounter floating point problems with such an equality comparison. You might want to do:
>
> find(abs(atan2(Y(:,2),Y(:,1))-somevalue)<tol) % For some tolerance.

Thanks guys, Ive come across the floating error thing before so I definitely will have to use the tolerance, but thanks a million for the posts!
Brendan

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