MATLAB Answers

0

WEIRD ERROR (INDEX EXCEEDS MATRIX DIMENSIONS)

Asked by Ripul Dutt on 7 Mar 2019
Latest activity Edited by Ripul Dutt on 7 Mar 2019
I am getting a weird error in the code, I am calling it weird becasue there's nothing wrong with the line of code where I am getting the error.
That line runs perfectly fine by itself. I am creating a matrix of zeros and then filling that matrix in a loop according to some conditions.
My code is below. Error on line 29, after the loop os run once!
%%%%CODE
%channel overlap method
run_hours = 560;
cd('C:\Users\Abdul Wahab\Desktop\Topo_data')
topo_data = load('ZdataD_final.mat');
topo = topo_data.ZD;
nx = 796; %number of x points
ny = 522; % number of y points
deriv=zeros(nx,ny,run_hours-1);%creation of a matrix to store the elevation change information
% creation of a volume of 1s and 0s
for k =1:1:run_hours-1
for i=1:1:nx
for j=1:1:ny
if(topo(i,j,k)-topo(i,j,k+1)>=1) || (topo(i,j,k)-topo(i,j,k+1)<=-1)
deriv(i,j,k)=1;
else
deriv(i,j,k)=0;
end
end
end
end
for k=1:1:run_hours-80
perc_change = zeros(80,2,run_hours-80);
for l=k:1:k+80
deriv_t=zeros(nx,ny);
for m=k:1:l
for i=1:1:nx
for j=1:1:ny
deriv_t(i,j)= deriv_t(i,j) + deriv(i,j,m);
end
end
end
% check_sum to assign all values greater than 1 to 1
for i=1:1:nx
for j=1:1:ny
if(deriv_t(i,j)>=1)
deriv_t(i,j)=1;
end
end
end
% calcuate the percentage of delta change upto a given l
ones=0;
zeros=0;
for i=1:1:nx
for j=1:1:ny
if(deriv_t(i,j)==1)
ones=ones+1;
else
zeros=zeros+1;
end
end
end
perc_change(l-k+1,1,k)=l-k+1;
perc_change(l-k+1,2,k)=ones/(ones+zeros);
end
end

  1 Comment

Can we have the data file for testing purposes?

Sign in to comment.

1 Answer

Answer by Niti K
on 7 Mar 2019

Try checking the limits on variable k in the second set of for loops.
k is defined from 1:run_hours-1 so deriv will have third dimension range from 1:559
From line 22 k will loop from 1:run_hours-80, however variable l(small L) in the for loop has a maximum range of run_hours(560). This is because of 'run_hours-80+80'
The for loop variable m is again dependent on k which will make deriv try to access 560th value in the third dimension. However deriv only has range up to 559.

  0 Comments

Sign in to comment.