|
"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
|