Thread Subject: Help with a basic for loop

Subject: Help with a basic for loop

From: NouveauIX

Date: 28 Sep, 2009 13:28:49

Message: 1 of 6

Hey, I'm having difficulty with a small problem utilizing a for loop. So far I have:

A=[];
for ind= length(Z) ;
    A=[Z(ind):-1:1];
end

Which is supposed to set variable A to be the reverse of vector Z. This works fine when Z is 1:10, but does not work correctly when Z is, say, [4 7 8]. A returns as:

8 7 6 5 4 3 2 1

So it's some small thing that my no-coffee brain can't figure out right now apparently.

Subject: Help with a basic for loop

From: Bruce Bowler

Date: 28 Sep, 2009 13:36:12

Message: 2 of 6

On Mon, 28 Sep 2009 09:28:49 -0400, NouveauIX wrote:

> Hey, I'm having difficulty with a small problem utilizing a for loop. So
> far I have:
>
> A=[];
> for ind= length(Z) ;
> A=[Z(ind):-1:1];
> end
>
> Which is supposed to set variable A to be the reverse of vector Z. This
> works fine when Z is 1:10, but does not work correctly when Z is, say,
> [4 7 8]. A returns as:
>
> 8 7 6 5 4 3 2 1
>
> So it's some small thing that my no-coffee brain can't figure out right
> now apparently.

I'm no matlab wizard, but that's exactly what I would expect it to
return. What were you expecting the results to be?

Bruce

Subject: Help with a basic for loop

From: dpb

Date: 28 Sep, 2009 13:33:33

Message: 3 of 6

NouveauIX wrote:
> Hey, I'm having difficulty with a small problem utilizing a for loop. So far I have:
>
> A=[];
> for ind= length(Z) ;
> A=[Z(ind):-1:1];
> end
>
> Which is supposed to set variable A to be the reverse of vector
> Z. This works fine when Z is 1:10, but does not work correctly when
> Z is,say, [4 7 8]. A returns as:
>
> 8 7 6 5 4 3 2 1
>
> So it's some small thing that my no-coffee brain can't figure out right now apparently.

You've got a step of -1 in the subscript expression that does that.

If the point is to reverse a vector, see

help fliplr

--

Subject: Help with a basic for loop

From: Steven Lord

Date: 28 Sep, 2009 13:39:07

Message: 4 of 6


"NouveauIX" <visualxd@gmail.com> wrote in message
news:2110906603.10608.1254144559704.JavaMail.root@gallium.mathforum.org...
> Hey, I'm having difficulty with a small problem utilizing a for loop. So
> far I have:
>
> A=[];
> for ind= length(Z) ;

This FOR loop is looping over one element, so it's the same as just
assigning ind = length(Z).

> A=[Z(ind):-1:1];

You're close, but instead of going from the last element of Z to 1 in steps
of -1, you want to go from the length of Z back to 1 in steps of -1 and then
use that vector to index into Z.

> end
>
> Which is supposed to set variable A to be the reverse of vector Z. This
> works fine when Z is 1:10, but does not work correctly when Z is, say, [4
> 7 8]. A returns as:
>
> 8 7 6 5 4 3 2 1
>
> So it's some small thing that my no-coffee brain can't figure out right
> now apparently.

You don't need to use a FOR loop for this. Just use FLIPLR, or A =
Z(end:-1:1). Alternately, if you're required to use a FOR loop (say if this
is part of an assignment) then preallocate A and then fill it in with the
appropriate values.

A = Z; % Preallocation to the right size and data type
L = length(Z);
for k = 1:L
    A(k) = Z(...)
end

I'll let you fill in the index into Z inside the loop.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Help with a basic for loop

From: someone

Date: 28 Sep, 2009 13:42:02

Message: 5 of 6

NouveauIX <visualxd@gmail.com> wrote in message <2110906603.10608.1254144559704.JavaMail.root@gallium.mathforum.org>...
> Hey, I'm having difficulty with a small problem utilizing a for loop. So far I have:
>
> A=[];
> for ind= length(Z) ;
> A=[Z(ind):-1:1];
> end
>
> Which is supposed to set variable A to be the reverse of vector Z. This works fine when Z is 1:10, but does not work correctly when Z is, say, [4 7 8]. A returns as:
>
> 8 7 6 5 4 3 2 1
>
> So it's some small thing that my no-coffee brain can't figure out right now apparently.

% You don't need a for loop. Simply:

A = Z(end:-1:1)

% or

A = fliplr(Z)

doc fliplr

Subject: Help with a basic for loop

From: NouveauIX

Date: 28 Sep, 2009 13:56:54

Message: 6 of 6

This works, thank you. The assignment gives a partially blank for loop and seems to imply that I can't add any lines, only fill in the blanks such as the inside of brackets, but to hell with that I guess. Had it just said to write the for loop then there wouldn't be as much of a problem :)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
fliplr someone 28 Sep, 2009 09:44:05
rssFeed for this Thread

Contact us at files@mathworks.com