I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:
for i = 2:9
dzu(i) = PPZU(i)-PPZU(i-1);
dzl(i) = PPZL(i)-PPZL(i-1);
dx(i) = PPX(i)-PPX(i-1);
end
The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead. Does anyone have any idea as to why this is happening? Thank you all in advance for your help.
"Ali Moradi" <kimusubi@gmail.com> wrote in message <hbfofq$9mi$1@fred.mathworks.com>...
> Hi,
>
> I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:
>
> for i = 2:9
> dzu(i) = PPZU(i)-PPZU(i-1);
> dzl(i) = PPZL(i)-PPZL(i-1);
> dx(i) = PPX(i)-PPX(i-1);
> end
>
> The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead.
No, length 9 is correct.
> Does anyone have any idea as to why this is happening?
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hbfpqs$4bp$1@fred.mathworks.com>...
> "Ali Moradi" <kimusubi@gmail.com> wrote in message <hbfofq$9mi$1@fred.mathworks.com>...
> > Hi,
> >
> > I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:
> >
> > for i = 2:9
> > dzu(i) = PPZU(i)-PPZU(i-1);
> > dzl(i) = PPZL(i)-PPZL(i-1);
> > dx(i) = PPX(i)-PPX(i-1);
> > end
> >
> > The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead.
>
> No, length 9 is correct.
>
> > Does anyone have any idea as to why this is happening?
>
> A simply setting
>
> d(1000)=1
>
> will give 1x1000 vector
>
> Bruno
I see what you mean, and I think I fixed it. I just added another loop and made it look like this:
for j = 1:8
for i = 2:9
dzu(j) = PPZU(i)-PPZU(i-1);
dzl(j) = PPZL(i)-PPZL(i-1);
dx(j) = PPX(i)-PPX(i-1);
end
end
"Ali Moradi" <kimusubi@gmail.com> wrote in message
news:hbfrmt$mi$1@fred.mathworks.com...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> <hbfpqs$4bp$1@fred.mathworks.com>...
>> "Ali Moradi" <kimusubi@gmail.com> wrote in message
>> <hbfofq$9mi$1@fred.mathworks.com>...
*snip*
> I see what you mean, and I think I fixed it. I just added another loop and
> made it look like this:
>
> for j = 1:8
> for i = 2:9
> dzu(j) = PPZU(i)-PPZU(i-1);
> dzl(j) = PPZL(i)-PPZL(i-1);
> dx(j) = PPX(i)-PPX(i-1);
> end
> end
>
> Does that look about right? Thanks again.
There's no need for a double loop here. Either use the loop from 2 to 9 and
remove the first element after the loop is finished:
dzu(1) = [];
or find some way to 'map' 2:9 to 1:8 and use that 'map' to convert the loop
variable into the appropriate index value. If you understand the following
example, you should be able to adapt it to your needs:
z = zeros(1, 10);
for k = 2:2:20
z(k/2) = k;
end
% Of course, if that was really your goal, you would just use z = 2:2:20;
"Ali Moradi" <kimusubi@gmail.com> wrote in message <hbfofq$9mi$1@fred.mathworks.com>...
> Hi,
>
> I'm writing a simple for loop for a homework problem and I'm coming across a weird problem that I can't figure out. I have 3 vectors all of size 9x1. I'm doing the following for loop:
>
> for i = 2:9
> dzu(i) = PPZU(i)-PPZU(i-1);
> dzl(i) = PPZL(i)-PPZL(i-1);
> dx(i) = PPX(i)-PPX(i-1);
> end
>
> The problem is that when I run it, I get a 9x1 vector as the answer, but it should be a 8x1 vector instead. Does anyone have any idea as to why this is happening? Thank you all in advance for your help.
Why loop at all? diff() will do your homework for you.
Tags for this Thread
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.
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central.
Read the complete Terms prior to use.