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:
Need help with a very simple loop

Subject: Need help with a very simple loop

From: James

Date: 9 Apr, 2010 06:53:05

Message: 1 of 9

I have an outline of a body. I have used CART2POL to convert the x and y coordinates to polar coordinates. Now i wish to find the radius for every angle.
Here is a simplified version of what i have so far:

THETA = [ 90; 45; -180; -135; 180; -45; 135; -90;]
R = [113; 122; 63; 89; 43; 11; 90; 52;]
for THETA = -180:45:180
     THETA, R
end;

However, this gives me all the R values for every THETA.
How do i write a loop that outputs a single value of R for every THETA? Say if i entered THETA = -135, it would return R = 89 instead of R = 113, 122, 63, 89, 43, 11, 90, 52.
Thanks, any help would be appreciated.

Subject: Need help with a very simple loop

From: us

Date: 9 Apr, 2010 06:59:06

Message: 2 of 9

"James " <cche5398@uni.sydney.edu.au> wrote in message <hpmish$i1g$1@fred.mathworks.com>...
> I have an outline of a body. I have used CART2POL to convert the x and y coordinates to polar coordinates. Now i wish to find the radius for every angle.
> Here is a simplified version of what i have so far:
>
> THETA = [ 90; 45; -180; -135; 180; -45; 135; -90;]
> R = [113; 122; 63; 89; 43; 11; 90; 52;]
> for THETA = -180:45:180
> THETA, R
> end;
>
> However, this gives me all the R values for every THETA.
> How do i write a loop that outputs a single value of R for every THETA? Say if i entered THETA = -135, it would return R = 89 instead of R = 113, 122, 63, 89, 43, 11, 90, 52.
> Thanks, any help would be appreciated.

one of the solutions

     t=[90;45;-180;-135;180;-45;135;-90];
     r=[113;122;63;89;43;11;90;52];
for i=1:numel(t)
      disp([t(i),r(i)]);
end

us

Subject: Need help with a very simple loop

From: James

Date: 9 Apr, 2010 15:32:07

Message: 3 of 9

"us " <us@neurol.unizh.ch> wrote in message <hpmj7q$mjp$1@fred.mathworks.com>...
> "James " <cche5398@uni.sydney.edu.au> wrote in message <hpmish$i1g$1@fred.mathworks.com>...
> > I have an outline of a body. I have used CART2POL to convert the x and y coordinates to polar coordinates. Now i wish to find the radius for every angle.
> > Here is a simplified version of what i have so far:
> >
> > THETA = [ 90; 45; -180; -135; 180; -45; 135; -90;]
> > R = [113; 122; 63; 89; 43; 11; 90; 52;]
> > for THETA = -180:45:180
> > THETA, R
> > end;
> >
> > However, this gives me all the R values for every THETA.
> > How do i write a loop that outputs a single value of R for every THETA? Say if i entered THETA = -135, it would return R = 89 instead of R = 113, 122, 63, 89, 43, 11, 90, 52.
> > Thanks, any help would be appreciated.
>
> one of the solutions
>
> t=[90;45;-180;-135;180;-45;135;-90];
> r=[113;122;63;89;43;11;90;52];
> for i=1:numel(t)
> disp([t(i),r(i)]);
> end
>
> us
Thank you very much for you time and effort, US.
Here is the output of your script:
90 113
45 122
-180 63
-135 89
180 43
-45 11
135 90
-90 52
This is only combining the two matrices. What i wanted to do with my loop is to return the corresponding radius whenever i enter a degree. For instance, if i input theta = 180, then i would like my loop to give me R = 43.
I have a similar but bigger file which contains around 400 different degrees and radius. To save the hassle of going through the bunch of numbers, i would like my loop to give me a radius whenever i input a degree.
 

Subject: Need help with a very simple loop

From: Rob Campbell

Date: 9 Apr, 2010 15:48:23

Message: 4 of 9


> What i wanted to do with my loop is to return the corresponding radius whenever i
enter a degree. For instance, if i input theta = 180, then i would like my loop to give me R = 43.

I don't think you want a loop. You want the code:
R(theta==180)

However, it seem to me that this is a strange way of doing things: you've essentially hard-coded a lot of numbers for no particular reason. The correct way to do this is to write a function that converts any arbitrary theta into r. Just re-write your existing code to do this. At the moment, if you ask for a theta which you haven't previously calculated then you'll get an error. You're also wasting memory and disk space storing numbers which you may never use.

Subject: Need help with a very simple loop

From: Matt Fig

Date: 9 Apr, 2010 16:15:24

Message: 5 of 9

Something like this(?):


% Data
THETA = [ 90; 45; -180; -135; 180; -45; 135; -90];
R = [113; 122; 63; 89; 43; 11; 90; 52];


% Engine
>> f = @(x) interp1(THETA,R,x); % See the help for INTERP1
>> f(180)
ans =
    43
>> f(-135)
ans =
    89

Subject: Need help with a very simple loop

From: David Young

Date: 9 Apr, 2010 17:12:10

Message: 6 of 9

and another:

for tr = [t'; r']
    disp(tr');
end

Subject: Need help with a very simple loop

From: David Young

Date: 9 Apr, 2010 17:17:04

Message: 7 of 9

Sorry, ignore my contribution - a bit behind the game.

Subject: Need help with a very simple loop

From: James

Date: 12 Apr, 2010 14:20:20

Message: 8 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <hpnjqs$21l$1@fred.mathworks.com>...
> Something like this(?):
>
>
> % Data
> THETA = [ 90; 45; -180; -135; 180; -45; 135; -90];
> R = [113; 122; 63; 89; 43; 11; 90; 52];
>
>
> % Engine
> >> f = @(x) interp1(THETA,R,x); % See the help for INTERP1
> >> f(180)
> ans =
> 43
> >> f(-135)
> ans =
> 89

Thank you very much Matt, this is exactly what i was looking for.
Here is another problem i have encountered. Don't know if you can help me with this one. Would be greatly appreciated if you can.
Here is my script:
THETA = [ 90.55; 45.123; 179.675; -180.12321; -135.7485; 180.123; -45.445; 135.9893; -90.343; 180.2323]
R = [113; 122; 29; 63; 89; 43; 11; 90; 52; 88]
TH = round(THETA);
f = @(x) interp1(TH,R,x); % See the help for INTERP1
f(180)
when i run the above script, i would get the following error:
??? Error using ==> interp1 at 261
The values of X should be distinct.

Error in ==> @(x)interp1(TH,R,x)

What is the best solution to this problem?
Should i average out the R values for all THETA = 180?
If so, how do i do it?

Subject: Need help with a very simple loop

From: James

Date: 12 Apr, 2010 14:21:04

Message: 9 of 9

"David Young" <d.s.young.notthisbit@sussex.ac.uk> wrote in message <hpnneg$q9t$1@fred.mathworks.com>...
> Sorry, ignore my contribution - a bit behind the game.

Thanks mate, i appreciate your contribution.

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