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:
erron in code

Subject: erron in code

From: Konst

Date: 12 Oct, 2012 15:18:11

Message: 1 of 5

Can someone tell me what is the error in this?
y=randn(500,1);
N=length(y);
f=1;
for t=10:30
n=f*t;
kapa=N/n;
kapa=round(kapa);
i=0;
clear alpha
clear alphadetrend
clear Y
while (i+n)<N
    for j=1:kapa
    alpha(1:n,j)=y(i+1:i+n,1);
    alphadetrend(1:n,j)=detrend(alpha(1:n,j));
    i=i+n;
    end
end
i=1;
while i<=N
    for j=1:kapa
        for k=1:n
            Y(i,1)=alphadetrend(k,j);
            i=i+1;
        end
    end
end
end

The first loop runs OK. But then when t becomes 11 it says that in line alpha(1:n,j)=y(i+1:i+n,1); index exceeds matrix dimensions. I tried to put an if to check the index i+1 too but it still gives the same error. Any ideas what is wrong?
Thanks in advance!

Subject: erron in code

From: dpb

Date: 12 Oct, 2012 16:44:09

Message: 2 of 5

On 10/12/2012 10:18 AM, Konst wrote:
> Can someone tell me what is the error in this?
> y=randn(500,1);
> N=length(y);
> f=1;
> for t=10:30
> n=f*t;
> kapa=N/n;
> kapa=round(kapa);
> i=0;
> clear alpha
> clear alphadetrend
> clear Y
> while (i+n)<N
> for j=1:kapa
> alpha(1:n,j)=y(i+1:i+n,1);
> alphadetrend(1:n,j)=detrend(alpha(1:n,j));
> i=i+n;
> end
> end
...

>
> The first loop runs OK. But then when t becomes 11 it says that in line
> alpha(1:n,j)=y(i+1:i+n,1); index exceeds matrix dimensions. I tried to
> put an if to check the index i+1 too but it still gives the same error.
> Any ideas what is wrong? Thanks in advance!

t
t =
     11
 >> j
j =
      5
 >> size(alpha)
ans =
     11 4
...

--

Subject: erron in code

From: Roger Stafford

Date: 12 Oct, 2012 20:20:27

Message: 3 of 5

"Konst " <konstance1@hotmail.com> wrote in message <k59cbj$2le$1@newscl01ah.mathworks.com>...
> The first loop runs OK. But then when t becomes 11 it says that in line alpha(1:n,j)=y(i+1:i+n,1); index exceeds matrix dimensions. .....
- - - - - - - - - -
  I don't see any error at t = 11, but at t = 12 you will have kapa = 42 and, because 12*42 = 504, at the last trip through the for-lop you will call on y(493:504) which is out of range. You had better change from round(kapa) to floor(kapa).

Roger Stafford

Subject: erron in code

From: Konst

Date: 13 Oct, 2012 14:56:07

Message: 4 of 5

"Roger Stafford" wrote in message <k59u2b$e4c$1@newscl01ah.mathworks.com>...
> "Konst " <konstance1@hotmail.com> wrote in message <k59cbj$2le$1@newscl01ah.mathworks.com>...
> > The first loop runs OK. But then when t becomes 11 it says that in line alpha(1:n,j)=y(i+1:i+n,1); index exceeds matrix dimensions. .....
> - - - - - - - - - -
> I don't see any error at t = 11, but at t = 12 you will have kapa = 42 and, because 12*42 = 504, at the last trip through the for-lop you will call on y(493:504) which is out of range. You had better change from round(kapa) to floor(kapa).
>
> Roger Stafford

But isn't the while supposed to control that I am below 500?
I changed to floor but it still gives me the same mistake at t=11. Then I did this:

    for j=1:kapa
      if (i+n)<N
      alpha(1:n,j)=y(i+1:i+n,1);
      alphadetrend(1:n,j)=detrend(alpha(1:n,j));
      i=i+n;
      end
    end
i=1;
    for j=1:kapa
        for k=1:n
            if i<=N
            Y(i,1)=alphadetrend(k,j);
            i=i+1;
            end
        end
    end
which I think does exactly the same but I was aimin to have better control over the indexes. Still the same error though.

Subject: erron in code

From: Roger Stafford

Date: 13 Oct, 2012 22:00:08

Message: 5 of 5

"Konst " <konstance1@hotmail.com> wrote in message <k5bve6$fte$1@newscl01ah.mathworks.com>...
> I changed to floor but it still gives me the same mistake at t=11. .....
 - - - - - - - -
  My advice to you would be to insert an appropriate 'fprintf' line immediately prior to your error-producing line to see what is going on at the time of the error, perhaps something like this:

    ...
    for j=1:kapa
    fprintf('i = %d, n = %d, size(y,1) = %d\n',i,n,size(y,1)) % <--
    alpha(1:n,j)=y(i+1:i+n,1);
    ...

You will get a lot of print-out lines but it will be the last one before the error that you will be interested in. It may lead you to the source of the trouble.

Roger Stafford

Tags for this Thread

No tags are associated with 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