MATLAB Answers

How to fix: Index Exceeds Matrix Dimensions

1.384 views (last 30 days)
Wouter Mattheussens
Wouter Mattheussens on 14 Sep 2017
Answered: Nazish Iqbal on 7 Jan 2020 at 19:21
Hi,
Im new to MATLAB and am trying to extract data from a sine wave. I've set the threshold to -1 SD and want all of the data below it to stack into one matrix. In other words, M has to be a stacked product of P. However, at the last part of the code it gives the error: Index Exceeds Matrix Dimensions. From what I understand, this means that length(P)+P(i)>length(noise). So the logical thing to do would be to make 'noise' bigger or P(i) smaller. However, I can't seem to get rid of the error. I was told not to index so far into P, but i have no idea how to do that. Hopefully someone can help me with this!
clear all
hold off
% parameters
srate=1000;
t=1:99/srate:10;
noiseAmplitude=2;
a=4;
f=4;
%signal
signal=a*sin(2*pi*f*t);
noise= signal + noiseAmplitude*randn(1,length(signal));
standdev=std(noise);
P=find(diff(noise<-standdev)==1);
for i=1:length(P)
M(i,:)=noise(P(i):P(i)+10); (<-- ERROR: index exceeds matrix dimensions)
end
plot(M)

  0 Comments

Sign in to comment.

Accepted Answer

Alexandra Harkai
Alexandra Harkai on 14 Sep 2017
P =
2 27 32 34 37 50 52 60 65 68 70 75 90
P+10
ans =
12 37 42 44 47 60 62 70 75 78 80 85 100
Therefore P+10 exceeds the size of noise, and is giving you the error.
How you go about fixing it depends what you want to achieve in those cases. This one would fill in the rightmost part of array M:
for i=1:length(P)
offset = min(10, size(noise,2)-P(i));
M(i,(end-offset):end)=noise(P(i):P(i)+offset);
end

  1 Comment

Wouter Mattheussens
Wouter Mattheussens on 15 Sep 2017
Thanks! This seems to be working for me. Although I also had to put M=zeros(1,100) before the for-loop.

Sign in to comment.

More Answers (6)

Cam Salzberger
Cam Salzberger on 14 Sep 2017
I don't fully understand what you are trying to do here, but I can tell you how to examine the issue. With any kind of error, it's often easiest to set a breakpoint at the line the error occurs. With an error in a loop, however, it could be many iterations before you actually hit the parameter combination that will cause the error. In that case, it's probably easiest to just run this at the command line:
dbstop if error
Now MATLAB will automatically go into debug mode as soon as it encounters an error. Once in debug mode, you can examine the variables and see where the issue is. Of course, in your particular code, the exact iteration may change every time because of the randomized noise.
I suspect that you don't actually want to index into "P" using "10" as an offset to the next index value, when "P" already contains index values itself. But I don't fully understand your workflow, so I can't be sure.
-Cam

  0 Comments

Sign in to comment.


Josh Meyer
Josh Meyer on 14 Sep 2017
Edited: Josh Meyer on 14 Sep 2017
The issue is with the line:
M(i,:)=noise(P(i):P(i)+10);
You want to take the index P(i) and grab the next 10 elements after that point, then put them into a row in M. This works fine for the first several iterations where P is picking out relatively small indices. However, some of the elements of P are picking out array elements near the end of noise, so there aren't 10 elements after those points to grab.
To put it more concretely, here is what P contains when I ran the code:
>> P
P =
2 12 14 20 27 30 32 40 55 65 70 75 78 80 88
So, when you get to the last element of P, 88, you will want to grab elements 88-98 from noise and put them into M. However, noise only has 91 elements:
>> size(noise)
ans =
1 91
So to fix this error you need to replace the +10 with something else. You can use min(diff(P)) to see the minimum distance between consecutive indices in P, or min(abs(P-length(noise))) to see how close the last element in P is to the end of noise, or maybe you could just loop to length(P)-1 or length(P)-2, avoiding the last few large indices in P.

  0 Comments

Sign in to comment.


yuvarani divakaramoorthy
yuvarani divakaramoorthy on 19 Sep 2018
for i=1:s/k sum=0; for j=1:k sum=m((i-1)*k+j)+sum; <--(ERROR: index exceeds matrix dimensions) end M(i)=sum/k;
what can be done here?

  0 Comments

Sign in to comment.


rubina naz
rubina naz on 22 Oct 2018
Rmin = 60; Rmax = 100; [center, radius] = imfindcircles(RGB,[Rmin Rmax],'Sensitivity',0.9); % Display the circle viscircles(center,radius); % Display the calculated center hold on; plot(center(:,1),center(:,2),'yx','LineWidth',2);%index axceed matrix problems hold off;

  0 Comments

Sign in to comment.


MathWorks Support Team
MathWorks Support Team on 28 Nov 2018
This error is returned when MATLAB tries to access elements of an array that do not exist. In this case, since “noise” only has 91 elements, MATLAB errors when the loop reaches an element of “P” such that “P(i) + 10” exceeds 91. Starting in R2018b, you will see the following error instead:
Index exceeds the number of array elements (91).
To fix this error, you will need to replace the index “P(i)+10” with something that doesn’t exceed 91, or end your loop earlier. For example, you could stop the loop at “length(P)-2”, and concatenate the remaining elements of “M” outside of the loop.

  0 Comments

Sign in to comment.


Nazish Iqbal
Nazish Iqbal on 7 Jan 2020 at 19:21
error.PNG
kindly help me regarding this error..
decryption.PNG

  0 Comments

Sign in to comment.

Sign in to answer this question.