http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831
MATLAB Central Newsreader  finding distances that satisfy the equation ? (confused :'( , please help)
Feed for thread: finding distances that satisfy the equation ? (confused :'( , please help)
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sun, 28 Mar 2010 18:12:22 +0000
finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730776
Natalie Sin Hwee
Hi all, i've generated a code as below: <br>
basically its 20 randomly placed nodes in a 1x1 square. i've then found a matrix of angles of each node and every other node (i.e. node 1 + node 2,3,4,..20)<br>
<br>
i then wrote: i want to find angles that fall within <br>
sect1=((0<thetasort) & (thetasort<(0.25*pi)));<br>
i.e. 0rad to pi/4 rad. <br>
returning me a matrix of 0 and 1's, (1's if they satisfy the eqn between 0pi/4rad)<br>
[sect1sort,R1]=sort(sect1,1) <br>
to sort... <br>
now:<br>
for those 1's (that satisfy), i want to find the distance between e.g. node 1 + those that satisfy, node 2 + those that satisfy etc)<br>
<br>
apologies for the confusion, its so hard to put it into words :(<br>
<br>
but i hope i can find a solution to this<br>
<br>
any other ideas are welcome!!<br>
<br>
cheers<br>
natalie<br>
<br>
<br>
========Script: ================<br>
a=20; %number of nodes<br>
<br>
% Randomly selecting values for x,y and zcoordinates<br>
x=rand(1,a); %generate 1xa sized matrix of random numbers between 0 and 1 <br>
y=rand(1,a);<br>
<br>
theta=zeros(a,a);<br>
for node=1:a; <br>
for u=1:a; <br>
theta(node,u)=atan2(imag((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]),...<br>
real((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]));<br>
end<br>
end<br>
<br>
[thetasort,R]=sort(theta,1); % increasing angles downward (column)<br>
<br>
sect1=((0<thetasort) & (thetasort<(0.25*pi)));

Sun, 28 Mar 2010 18:31:02 +0000
Re: finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730781
us
"Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <hoo666$8u3$1@fred.mathworks.com>...<br>
> Hi all, i've generated a code as below: <br>
> basically its 20 randomly placed nodes in a 1x1 square. i've then found a matrix of angles of each node and every other node (i.e. node 1 + node 2,3,4,..20)<br>
> <br>
> i then wrote: i want to find angles that fall within <br>
> sect1=((0<thetasort) & (thetasort<(0.25*pi)));<br>
> i.e. 0rad to pi/4 rad. <br>
> returning me a matrix of 0 and 1's, (1's if they satisfy the eqn between 0pi/4rad)<br>
> [sect1sort,R1]=sort(sect1,1) <br>
> to sort... <br>
> now:<br>
> for those 1's (that satisfy), i want to find the distance between e.g. node 1 + those that satisfy, node 2 + those that satisfy etc)<br>
> <br>
> apologies for the confusion, its so hard to put it into words :(<br>
> <br>
> but i hope i can find a solution to this<br>
> <br>
> any other ideas are welcome!!<br>
> <br>
> cheers<br>
> natalie<br>
> <br>
> <br>
> ========Script: ================<br>
> a=20; %number of nodes<br>
> <br>
> % Randomly selecting values for x,y and zcoordinates<br>
> x=rand(1,a); %generate 1xa sized matrix of random numbers between 0 and 1 <br>
> y=rand(1,a);<br>
> <br>
> theta=zeros(a,a);<br>
> for node=1:a; <br>
> for u=1:a; <br>
> theta(node,u)=atan2(imag((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]),...<br>
> real((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]));<br>
> end<br>
> end<br>
> <br>
> [thetasort,R]=sort(theta,1); % increasing angles downward (column)<br>
> <br>
> sect1=((0<thetasort) & (thetasort<(0.25*pi)));<br>
<br>
a hint:<br>
 create a list [IR,IC] of those nodes only that satisfy your condition...<br>
 then, compute their distance in a simple loop...<br>
<br>
[ir,ic]=find(sect1);<br>
lst=[ir,ic];<br>
% now, loop through your list LST...<br>
<br>
just a thought...<br>
us

Sun, 28 Mar 2010 19:10:21 +0000
Re: finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730787
Roger Stafford
"Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <hoo666$8u3$1@fred.mathworks.com>...<br>
> ........<br>
> for node=1:a; <br>
> for u=1:a; <br>
> theta(node,u)=atan2(imag((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]),...<br>
> real((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]));<br>
> end<br>
> end<br>
> <br>
> [thetasort,R]=sort(theta,1); % increasing angles downward (column)<br>
> <br>
> sect1=((0<thetasort) & (thetasort<(0.25*pi)));<br>
<br>
This looks like a very awkward way to compute your angles. Why take the trouble to create complexvalued quantities only to convert them back to real values again? Why not just say this:<br>
<br>
theta(node,u)=atan2(y(u)y(node),x(u)x(node));<br>
<br>
With either method you will be obtaining angles measured counterclockwise from the xaxis to the directed line between each pair of points and which range from pi to +pi. You will be selecting only those between 0 and +pi/4, and in particular rejecting those between pi/4 and 0. In the cases where node equals u along the diagonal your results will be meaningless. Is this what you want?<br>
<br>
Why do you sort the angles before subjecting them to your range tests? What you will get is a series of falses, then trues, and finally falses. As your code stands there is no way to associate this with the original data in x and y.<br>
<br>
Roger Stafford

Sun, 28 Mar 2010 19:42:02 +0000
Re: finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730797
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hoo9it$r4p$1@fred.mathworks.com>...<br>
> .......<br>
> Why do you sort the angles before subjecting them to your range tests? What you will get is a series of falses, then trues, and finally falses. As your code stands there is no way to associate this with the original data in x and y.<br>
> .......<br>
<br>
Correct that last sentence. You could use your 'R' to relate the results in 'sect1' to the original data, but I still ask, why do the sort?<br>
<br>
Roger Stafford

Mon, 29 Mar 2010 09:56:04 +0000
Re: finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730971
Natalie Sin Hwee
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hoo9it$r4p$1@fred.mathworks.com>...<br>
> "Natalie Sin Hwee " <sin.ng09@imperial.ac.uk> wrote in message <hoo666$8u3$1@fred.mathworks.com>...<br>
> > ........<br>
> > for node=1:a; <br>
> > for u=1:a; <br>
> > theta(node,u)=atan2(imag((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]),...<br>
> > real((x(1,u)x(1,node))+[(y(1,u)y(1,node))*i]));<br>
> > end<br>
> > end<br>
> > <br>
> > [thetasort,R]=sort(theta,1); % increasing angles downward (column)<br>
> > <br>
> > sect1=((0<thetasort) & (thetasort<(0.25*pi)));<br>
> <br>
> This looks like a very awkward way to compute your angles. Why take the trouble to create complexvalued quantities only to convert them back to real values again? Why not just say this:<br>
> <br>
> theta(node,u)=atan2(y(u)y(node),x(u)x(node));<br>
> <br>
> With either method you will be obtaining angles measured counterclockwise from the xaxis to the directed line between each pair of points and which range from pi to +pi. You will be selecting only those between 0 and +pi/4, and in particular rejecting those between pi/4 and 0. In the cases where node equals u along the diagonal your results will be meaningless. Is this what you want?<br>
> <br>
> Why do you sort the angles before subjecting them to your range tests? What you will get is a series of falses, then trues, and finally falses. As your code stands there is no way to associate this with the original data in x and y.<br>
> <br>
> Roger Stafford<br>
<br>
hiya, <br>
<br>
basically i used a more 'complex' method because im working from 0pi and 0  (pi) hence the atan2. im nore sure why im using the real and imaginary. apparently it will work in 3d (through my previous quetisons on the forum)  which i will later need to make my model into 3d !! ^^ <br>
<br>
thanks <br>
natalie

Mon, 29 Mar 2010 09:57:02 +0000
Re: finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730973
Natalie Sin Hwee
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hoobea$nnq$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <hoo9it$r4p$1@fred.mathworks.com>...<br>
> > .......<br>
> > Why do you sort the angles before subjecting them to your range tests? What you will get is a series of falses, then trues, and finally falses. As your code stands there is no way to associate this with the original data in x and y.<br>
> > .......<br>
> <br>
> Correct that last sentence. You could use your 'R' to relate the results in 'sect1' to the original data, but I still ask, why do the sort?<br>
> <br>
> Roger Stafford<br>
<br>
=========<br>
<br>
Oh you are right, i don't need the sorting out! <br>
i was so clueless and stuck so i just did whatever randomly ^^<br>
<br>
thanks for reminding me, took it out now!!

Mon, 29 Mar 2010 10:01:07 +0000
Re: finding distances that satisfy the equation ? (confused :'( , please help)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/277831#730974
Natalie Sin Hwee
> a hint:<br>
>  create a list [IR,IC] of those nodes only that satisfy your condition...<br>
>  then, compute their distance in a simple loop...<br>
> <br>
> [ir,ic]=find(sect1);<br>
> lst=[ir,ic];<br>
> % now, loop through your list LST...<br>
> <br>
> just a thought...<br>
> us<br>
<br>
=====<br>
<br>
thanks us!! i did that and it works!!!!! awesome!! ive added in:<br>
<br>
sect1=((0<theta) & (theta<(0.25*pi)));<br>
<br>
[g1,k1]=find(sect1>=1); first=[g1,k1];<br>
first_sort=sort(first,2);<br>
<br>
L1=length(k1); <br>
dist1=zeros(L1,1);<br>
for n=1:L1<br>
dist1(n,1)=sqrt((x(1,first_sort(n,2))x(1,first_sort(n,1))).^2+...<br>
(y(1,first_sort(n,2))y(1,first_sort(n,1))).^2);<br>
end<br>
<br>
<br>
and that returns me a the 'distance between nodes' corresponding to what i've found in 'first'  column1 wrt to column 2 'nodes'<br>
<br>
any idea how i can separate them out into the different 'nodes' wrt to column 1. e.g.<br>
<br>
node 1 + 2,7,8,9,(random whatever it has given me)<br>
<br>
my aim is to find,<br>
<br>
the nearest node to each node within that angle (0 to pi/4)...<br>
<br>
(will be repeated as applied to the 8 sectors in the circle) <br>
<br>
*omg sorry this is getting more and more confusing !! ^^ <br>
<br>
thanks lots just for reading through it!<br>
Natalie