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:
keep array in for loop

Subject: keep array in for loop

From: Chris

Date: 10 Mar, 2009 19:52:02

Message: 1 of 3

Hi,
I have a for loop which calculates and array each time and I would like to keep each of these, but can't seem to get it to work at the moment - perhaps I'm tired!
Anyway, an example below.
Cheers,
Chris

q=(0:0.01:2)*pi;
for n=1:2
    x(n)=n*cos(q);
    y(n)=n*sin(q);
    plot(x.n,y.n)
    hold on
end
hold off

Subject: keep array in for loop

From: Matt Fig

Date: 10 Mar, 2009 20:18:02

Message: 2 of 3

Maybe something like this:


q = (0:0.01:2)*pi;
for n = 2:-1:1
    x(:,n) = n*cos(q);
    y(:,n) = n*sin(q);
end
plot(x,y)
axis equal

which can also be written as:

q = (0:0.01:2)*pi;
cq = cos(q);
sq = sin(q);
n = diag([1 2]);
x = n*cq(ones(1,length(n)),:);
y = n*sq(ones(1,length(n)),:);
plot(x',y')
axis equal




cd4Ua\.aUYcscWisss"`]szAV[mUjUUhbs\iVbhYsdU\z_9YY]ccWUMs`ch

Subject: keep array in for loop

From: Chris

Date: 10 Mar, 2009 21:58:01

Message: 3 of 3

Thats the badger, cheers for that!
Problem solved :)
"Matt Fig" <spamanon@yahoo.com> wrote in message <gp6htq$adr$1@fred.mathworks.com>...
> Maybe something like this:
>
>
> q = (0:0.01:2)*pi;
> for n = 2:-1:1
> x(:,n) = n*cos(q);
> y(:,n) = n*sin(q);
> end
> plot(x,y)
> axis equal
>
> which can also be written as:
>
> q = (0:0.01:2)*pi;
> cq = cos(q);
> sq = sin(q);
> n = diag([1 2]);
> x = n*cq(ones(1,length(n)),:);
> y = n*sq(ones(1,length(n)),:);
> plot(x',y')
> axis equal
>
>
>
>
> cd4Ua\.aUYcscWisss"`]szAV[mUjUUhbs\iVbhYsdU\z_9YY]ccWUMs`ch

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