MATLAB Answers

How to fix: Index Exceeds Matrix Dimensions

1.453 views (last 30 days)
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 (10)

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.


dawa lepcha
dawa lepcha on 3 Feb 2020
Hi , i have an error in line 18,
kindly need helps
>> guidedfilter_color
Index exceeds matrix dimensions.
Error in guidedfilter_color (line 18)
mean_I_g = boxfilter(I(:, :, 2), r) ./ N;
function q = guidedfilter_color(I, p, r, eps)
% GUIDEDFILTER_COLOR O(1) time implementation of guided filter using a color image as the guidance.
%
% - guidance image: I (should be a color (RGB) image)
% - filtering input image: p (should be a gray-scale/single channel image)
% - local window radius: r
% - regularization parameter: eps
I = double(imread('.\img_smoothing\cat.bmp')) / 255;
p = I;
r = 4; % try r=2, 4, or 8
eps = 0.2^2; % try eps=0.1^2, 0.2^2, 0.4^2
[hei, wid] = size(p);
N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
mean_I_r = boxfilter(I(:, :, 1), r) ./ N;
mean_I_g = boxfilter(I(:, :, 2), r) ./ N;
mean_I_b = boxfilter(I(:, :, 3), r) ./ N;
mean_p = boxfilter(p, r) ./ N;
mean_Ip_r = boxfilter(I(:, :, 1).*p, r) ./ N;
mean_Ip_g = boxfilter(I(:, :, 2).*p, r) ./ N;
mean_Ip_b = boxfilter(I(:, :, 3).*p, r) ./ N;
% covariance of (I, p) in each local patch.
cov_Ip_r = mean_Ip_r - mean_I_r .* mean_p;
cov_Ip_g = mean_Ip_g - mean_I_g .* mean_p;
cov_Ip_b = mean_Ip_b - mean_I_b .* mean_p;
% variance of I in each local patch: the matrix Sigma in Eqn (14).
% Note the variance in each local patch is a 3x3 symmetric matrix:
% rr, rg, rb
% Sigma = rg, gg, gb
% rb, gb, bb
var_I_rr = boxfilter(I(:, :, 1).*I(:, :, 1), r) ./ N - mean_I_r .* mean_I_r;
var_I_rg = boxfilter(I(:, :, 1).*I(:, :, 2), r) ./ N - mean_I_r .* mean_I_g;
var_I_rb = boxfilter(I(:, :, 1).*I(:, :, 3), r) ./ N - mean_I_r .* mean_I_b;
var_I_gg = boxfilter(I(:, :, 2).*I(:, :, 2), r) ./ N - mean_I_g .* mean_I_g;
var_I_gb = boxfilter(I(:, :, 2).*I(:, :, 3), r) ./ N - mean_I_g .* mean_I_b;
var_I_bb = boxfilter(I(:, :, 3).*I(:, :, 3), r) ./ N - mean_I_b .* mean_I_b;
a = zeros(hei, wid, 3);
for y=1:hei
for x=1:wid
Sigma = [var_I_rr(y, x), var_I_rg(y, x), var_I_rb(y, x);
var_I_rg(y, x), var_I_gg(y, x), var_I_gb(y, x);
var_I_rb(y, x), var_I_gb(y, x), var_I_bb(y, x)];
%Sigma = Sigma + eps * eye(3);
cov_Ip = [cov_Ip_r(y, x), cov_Ip_g(y, x), cov_Ip_b(y, x)];
a(y, x, :) = cov_Ip * inv(Sigma + eps * eye(3)); % Eqn. (14) in the paper;
end
end
b = mean_p - a(:, :, 1) .* mean_I_r - a(:, :, 2) .* mean_I_g - a(:, :, 3) .* mean_I_b; % Eqn. (15) in the paper;
q = (boxfilter(a(:, :, 1), r).* I(:, :, 1)...
+ boxfilter(a(:, :, 2), r).* I(:, :, 2)...
+ boxfilter(a(:, :, 3), r).* I(:, :, 3)...
+ boxfilter(b, r)) ./ N; % Eqn. (16) in the paper;
figure();imshow(q)
end

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 3 Feb 2020
Bmp files are not always rgb.
And when they are rgb then
[hei, wid] = size(p);
is always wrong.
[hei, wid, panes] = size(p);

  0 Comments

Sign in to comment.


Getnet Belie
Getnet Belie on 14 Jun 2020
Edited: Walter Roberson on 14 Jun 2020
Index exceeds matrix dimensions.
Error in feasmbll (line 21)
kk(ii,jj)=kk(ii,jj)+k(i,j)
function [kk]=feasmbll(kk,k,index)
%..................
%purpose;
%assembly of element matrices into the system matricx
%
% Synopsis;
%[kk]=feasmbll(kk,k,index)
%
%Variable Description;
%kk-system matrix
%k-element matrix
%index-d.o.f.vector associated with an element
%.................
%
edof=length(index);
%
for i=1:edof
ii=index(i);
for j=1:edof
jj=index(j);
26. kk(ii,jj)=kk(ii,jj)+k(i,j)
end
end
end
could you help me
kindly

  1 Comment

Walter Roberson
Walter Roberson on 14 Jun 2020
We do not know what size(kk) or size(k) are.
Is the 26. part of the code?

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
Edited: Walter Roberson on 14 Jun 2020
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
error.PNG
kindly help me regarding this error..
decryption.PNG

  0 Comments

Sign in to comment.


Abdul Basit
Abdul Basit on 15 Jun 2020
clc;
clear
close all;
%% Phase 1 - Define Objective Function
%% Phase 2 - PSO Parameters
LB=[0 0 0];
UB=[10 10 10];
m=3;
n=500;
wmax=0.9;
wmin=0.4;
c1=2.05;
c2=2.05;
Maxiter=100;
% Loop for maximum run start
for run=1:20
%% Phase 3 - Initialization of Position & Velocity
for i=1:n
for j=1:m
pos(i,j)=(LB(i,j)+rand()).*(UB(i,j)-LB(i,j)); <===Index exceeds matrix dimensions
Please some one help as at this point i see the error: Index exceeds matrix dimension
end
end
vel=(0.1).*pos;
%% Phase 4 - Function Evaluation
for i=1:n
out(i,1)=fun(pos(i,:));
end
pbestval=out;
pbest=pos;
[fminval, index]=min(out);
gbest=pbest(index,:);
%PSO algorithm start here
iter=1;
while iter<=Maxiter
w=wmax-(iter/Maxiter).*(wmax-wmin);
%% Phase 5 - Compute PBest and GBest
X=pos;
Out=fun(X);
Har=find(out<=pbestval);
pbest(Har,:)=X(Har,:);
pbestval=out(Har);
[fbestval, ind1]=min(pbestval);
if fbestval<=fminval
fminvalue=fbestval;
gbest=pbest(ind1,:);
end
%% Phase 6 - Update Velocity & Position (Handling Boundary Constrains)
for i=1:n
for j=1:m
vel(i,j)=w.*vel(i,j)+c1.*rand().*(pbest(i,j)-pos(i,j))...
+c2.*rand().*(gbeat(1,j)-pos(i,j));
pos(i,j)=vel(i,j)+pos(i,j);
if pos(i,j)<LB(j)
pos(i,j)=LB(j);
elseif pos(i,j)>UB(j)
pos(i,j)=UB(j);
end
end
end
iter=iter+1;
end
%% Phase 7 - Store Best Value
F_ans(run)=fun(gbest);
F_gbest(run,:)=gbest;
% Loop for maximum run end
end
[bestFUN, bestRUN]=min(F_ans);
Best_X=F_gbest(bestRUN,:);
plot(F_ans)
function output = fun(X)
fx=10*(x1-1)^2+20.*x1.*x2+(x3-3)^2;
Con=[];
% Con(1)=x1+x2+x3-5;
% Con(2)=x1^2+x2^2-x3;
%
% for i=1:length(Con)
% if Con(i)>0
% Pen(i)=1;
% else
% Pen(i)=0;
% end
% end
% penalty=1000;
%output=fx+penalty*sum(pen);
output=fx;
end

  10 Comments

Show 7 older comments
Walter Roberson
Walter Roberson on 15 Jun 2020
function output = fun(X)
fx=10*(x1-1)^2+20.*x1.*x2+(x3-3)^2;
That code attempts to use x1, x2, and x3, but those do not exist in that context: there is just X .
The code should probably be using X(1), X(2), X(3)
Abdul Basit
Abdul Basit on 15 Jun 2020
Brilliant, Many thanks. I look forward to disscuss more with you. Words do not seems enough to say thank you.
Abdul Basit
Abdul Basit on 15 Jun 2020
Sorry to be pain, Can you please help here with the last code error:
clc;
clear
close all;
%% Phase 1 - Objective Function
%fun
%% Phase 2 - PSO Parameters
LB=[0 0 0];
UB=[10 10 10];
m=3;
n=500;
wmax=0.9;
wmin=0.4;
c1=2.05;
c2=2.05;
Maxiter=100;
% Loop for maximum run start
for run=1:20
%% Phase 3 - Initialization of Position & Velocity
for i=1:n
for j=1:m
pos(i,j)=(LB(j)+rand()).*(UB(j)-LB(j));
end
end
vel=(0.1).*pos;
%% Phase 4 - Function Evaluation
for i=1:n
out(i,1)=fun(pos(i,:));
end
pbestval=out;
pbest=pos;
[fminval, index]=min(out);
gbest=pbest(index,:);
%PSO algorithm start here
iter=1;
while iter<=Maxiter
w=wmax-(iter/Maxiter).*(wmax-wmin);
%% Phase 5 - Compute PBest and GBest
X=pos;
Out=fun(X);
Har=find(out<=pbestval);
pbest(Har,:)=X(Har,:);
pbestval=out(Har);
[fbestval, ind1]=min(pbestval);
if fbestval<=fminval
fminvalue=fbestval;
gbest=pbest(ind1,:);
end
%% Phase 6 - Update Velocity & Position (Handling Boundary Constrains)
for i=1:n
for j=1:m
vel(i,j)=w.*vel(i,j)+c1.*rand().*(pbest(i,j)-pos(i,j))...
+c2.*rand().*(gbest(1,j)-pos(i,j));
pos(i,j)=vel(i,j)+pos(i,j);
if pos(i,j)<LB(j)
pos(i,j)=LB(j);
elseif pos(i,j)>UB(j)
pos(i,j)=UB(j);
end
end
end
iter=iter+1;
end
%% Phase 7 - Store Best Value
F_ans(run)=fun(gbest);
F_gbest(run,:)=gbest;
% Loop for maximum run end
end
[bestFUN, bestRUN]=min(F_ans);
Best_X=F_gbest(bestRUN,:);
plot(F_ans)
Function
function output = fun(X)
x=X(:,1);
R=X(:,2);
H=X(:,3);
vc=3; %Cut in Speed(m/s)
vf=25; %Cut Out Speed(m/s)
vr=12; %Rated Wind Speed(m/s)
p=1.2754; %value of air density i.e.:1.2754kg/m^3
% R=36; %Value of Rotor Radius (m)
% H=65; %Value of Hub Height (m)
Pr=1194.7; %Value of Rated Power(kW)
k=2; %Shape Factor:
c=7.5; %Scale Factor
fun1 = @(v)(v.^3).*(k/c).*((v/c).^(k-1)).*exp(-(v/c).^k);
fun2 = @(v)(vr.^3).*(k/c).*((v/c).^(k-1)).*exp(-(v/c).^k);
f1 = integral(fun1,vc,vr);
f2 = integral(fun2,vr,vf);
AEP=((1744.992.*p.*(pi).*(R.^2))).*(f1+f2);
a=(0.00622.*(R.^3.5))+(0.07756.*(R.^3))+(0.0612.*(R.^2.946))+(0.009426.*(R.^2.887))+(0.3508.*(R.^2.6578));
b=(1.3377.*(R.^2.53))+(0.5297.*(R.^2.5025))-(0.00771.*(R.^2.5))+(0.06901.*(R.^2).*H.*pi)+(5.343.*(R.^1.953));
c=(23.9347.*(R))+(55.7435.*(H.^0.4037).*(R.^0.8074))+((4.041.*(10.^-7)).*(Pr.^3))-(0.00244.*(Pr.^2))+(2.096.*(Pr.^1.249));
d=(15.0656.*(Pr))+(6.8004.*(Pr))+(((0.00108).*(AEP)).*(1-x/15000))+(8265.6416);
e=((0.2082.*(Pr.^3))-(0.006.*(Pr.^3))+(49.4518.*(Pr))+(0.5133.*(H.^1.1736).*(R.^1.1736))).*(1+x/15000);
f=(0.007.*(1+(x/15000)));
output=((a/AEP)+(b/AEP)+(c/AEP)+(d/AEP)+(e/AEP)+f); <== Error Here
end
Error using +
Matrix dimensions must agree.
Error in fun (line 58)
output=((a/AEP)+(b/AEP)+(c/AEP)+(d/AEP)+(e/AEP)+f);
Error in PSOnew (line 56)
Out=fun(X);

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by