MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: Natalie Sin Hwee Date: 28 Mar, 2010 18:12:22 Message: 1 of 7 Hi all, i've generated a code as below: 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) i then wrote: i want to find angles that fall within sect1=((0
 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: us Date: 28 Mar, 2010 18:31:02 Message: 2 of 7 "Natalie Sin Hwee " wrote in message ... > Hi all, i've generated a code as below: > 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) > > i then wrote: i want to find angles that fall within > sect1=((0 i.e. 0rad to pi/4 rad. > returning me a matrix of 0 and 1's, (1's if they satisfy the eqn between 0-pi/4rad) > [sect1sort,R1]=sort(sect1,1) > to sort... > now: > 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) > > apologies for the confusion, its so hard to put it into words :( > > but i hope i can find a solution to this > > any other ideas are welcome!! > > cheers > natalie > > > ========Script: ================ > a=20; %number of nodes > > % Randomly selecting values for x,y and z-coordinates > x=rand(1,a); %generate 1xa sized matrix of random numbers between 0 and 1 > y=rand(1,a); > > theta=zeros(a,a); > for node=1:a; > for u=1:a; > theta(node,u)=atan2(imag((x(1,u)-x(1,node))+[(y(1,u)-y(1,node))*i]),... > real((x(1,u)-x(1,node))+[(y(1,u)-y(1,node))*i])); > end > end > > [thetasort,R]=sort(theta,1); % increasing angles downward (column) > > sect1=((0
 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: Roger Stafford Date: 28 Mar, 2010 19:10:21 Message: 3 of 7 "Natalie Sin Hwee " wrote in message ... > ........ > for node=1:a; > for u=1:a; > theta(node,u)=atan2(imag((x(1,u)-x(1,node))+[(y(1,u)-y(1,node))*i]),... > real((x(1,u)-x(1,node))+[(y(1,u)-y(1,node))*i])); > end > end > > [thetasort,R]=sort(theta,1); % increasing angles downward (column) > > sect1=((0
 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: Roger Stafford Date: 28 Mar, 2010 19:42:02 Message: 4 of 7 "Roger Stafford" wrote in message ... > ....... > 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. > .......   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? Roger Stafford
 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: Natalie Sin Hwee Date: 29 Mar, 2010 09:56:04 Message: 5 of 7 "Roger Stafford" wrote in message ... > "Natalie Sin Hwee " wrote in message ... > > ........ > > for node=1:a; > > for u=1:a; > > theta(node,u)=atan2(imag((x(1,u)-x(1,node))+[(y(1,u)-y(1,node))*i]),... > > real((x(1,u)-x(1,node))+[(y(1,u)-y(1,node))*i])); > > end > > end > > > > [thetasort,R]=sort(theta,1); % increasing angles downward (column) > > > > sect1=((0 ---------- > This looks like a very awkward way to compute your angles. Why take the trouble to create complex-valued quantities only to convert them back to real values again? Why not just say this: > > theta(node,u)=atan2(y(u)-y(node),x(u)-x(node)); > > With either method you will be obtaining angles measured counterclockwise from the x-axis 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? > > 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. > > Roger Stafford hiya, basically i used a more 'complex' method because im working from 0-pi 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 !! ^^ thanks natalie
 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: Natalie Sin Hwee Date: 29 Mar, 2010 09:57:02 Message: 6 of 7 "Roger Stafford" wrote in message ... > "Roger Stafford" wrote in message ... > > ....... > > 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. > > ....... > > 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? > > Roger Stafford ========= Oh you are right, i don't need the sorting out! i was so clueless and stuck so i just did whatever randomly ^^ thanks for reminding me, took it out now!!
 Subject: finding distances that satisfy the equation ? (confused :'( , please help) From: Natalie Sin Hwee Date: 29 Mar, 2010 10:01:07 Message: 7 of 7 > a hint: > - create a list [IR,IC] of those nodes only that satisfy your condition... > - then, compute their distance in a simple loop... > > [ir,ic]=find(sect1); > lst=[ir,ic]; > % now, loop through your list LST... > > just a thought... > us ===== thanks us!! i did that and it works!!!!! awesome!! ive added in: sect1=((0=1); first=[g1,k1]; first_sort=sort(first,2); L1=length(k1); dist1=zeros(L1,1); for n=1:L1 dist1(n,1)=sqrt((x(1,first_sort(n,2))-x(1,first_sort(n,1))).^2+...     (y(1,first_sort(n,2))-y(1,first_sort(n,1))).^2); end and that returns me a the 'distance between nodes' corresponding to what i've found in 'first' - column1 wrt to column 2 'nodes' any idea how i can separate them out into the different 'nodes' wrt to column 1. e.g. node 1 + 2,7,8,9,(random whatever it has given me) my aim is to find, the nearest node to each node within that angle (0 to pi/4)... (will be repeated as applied to the 8 sectors in the circle) *omg sorry this is getting more and more confusing !! ^^ thanks lots just for reading through it! Natalie