Index exceeds matrix dimensions

3 visualizzazioni (ultimi 30 giorni)
Rhys Webb
Rhys Webb il 19 Set 2017
Modificato: Rhys Webb il 19 Set 2017
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).

Risposta accettata

KL
KL il 19 Set 2017
Modificato: KL il 19 Set 2017
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 Commento
Rhys Webb
Rhys Webb il 19 Set 2017
Modificato: Rhys Webb il 19 Set 2017
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by