Index exceeds matrix dimensions

조회 수: 2 (최근 30일)
Rhys Webb
Rhys Webb 2017년 9월 19일
편집: Rhys Webb 2017년 9월 19일
I am having an issue with the index exceeds matrix dimensions which for the life of me I can't understand because this is very similar (almost identical) to a piece of code I have previously written for the same assignment task which worked perfectly only the data has been scaled up to be metres as opposed to millimetres.
The data for the dam depths is a 36 x 20 array
a = xlsread('C:\Users\Rhys\Desktop\Matlab assignment\Task 2\dam_data.xlsx')*-1; %load data and correct orrientation
[X,Y] = meshgrid(1600:200:5400,2400:200:9400); %grid for measurement incrememnts (scaled 1:10000 and converted to m)
H_max = min(min(a)); %deepest point in dam
h_current = 16.2; %current dam water level
b = a<0; %determine points below dam wall height
c = a.*b; %correcting data to show points only below dam wall height
e = 0;
for i = 1 : X - 1 %determine to scale capacity
e = e + (-c(i+1) + -c(i))/4 * (X(i+1)-X(i));
for j = 1 : Y - 1
e = e + (-c(j+1) + -c(j))/4 * (Y(j+1)-Y(j));
end
end
the offending line of code is which continues in a loop until it gets to 720 iterations then stops is
for j = 1 : Y - 1
e = e + (-c(j+1) + -c(j))/4 * (Y(j+1)-Y(j));
end
The error message I get is
Index exceeds matrix dimensions.
Error in assignment_code (line 19) e = e + (-c(j+1) + -c(j))/4 * (Y(j+1)-Y(j));
Any help with this would be appreciated as for the most part what I have learnt has been through google and I am really struggling with this one (I managed to fix by pure fluke last time I had this issue).

채택된 답변

KL
KL 2017년 9월 19일
편집: KL 2017년 9월 19일
If your data is a 36x20 array, then a,b and then c will all be 36x20. But your i and j are from 1 to 1599/2399. That's why you get the error. Are we missing anything?
  댓글 수: 1
Rhys Webb
Rhys Webb 2017년 9월 19일
편집: Rhys Webb 2017년 9월 19일
This one answer has helped me understand how to solve this issue more than my lecturer has taught in an entire semester thank you.
I have asked this question of both my lecturer and tutor and neither could really provide an answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by