Array indices must be positive integers or logical values.

I uploaded my excel file and matlab code.
I want to portray the value of the elevation (El1). But to do this, what I need to do is, The value of Elevation in cell D5 should be the same value of elevation in cell L2. Then the value of the suceeding elevation should be calculated as, the value of the previous elevation minus the value of the difference of Db1 and Da1.
To further explain, the value of the elevation is 6m. Therefore, the elevation in cell D5 should be 6m as well. But the value of the elevation in cell D6 should be like this:
cell D6 = (cell D5 - 1) - (Db1 - Da1). And it should continue on until the last cell of elevation is computed. But of course, it should portray the negative value. So in cell D13, the elevation should be a negative number. I don't know how to portray the value.

 Réponse acceptée

Yutaka Yamada
Yutaka Yamada le 24 Mai 2021
Modifié(e) : Yutaka Yamada le 24 Mai 2021
It seems that Elev1 is the initial elevation.
I think it's better to get the array size first like below. Otherwise the array size of Db1 and El1 becoems different.
N = length(Db1);
El1 = zeros(N,1);
El1(1) = Elev1;
%elevation
for i = 1:N-1
El1(i+1) = El1(i) - (Db1(i+1) - Da1(i+1));
end
By running above script, you can get the below El1.

5 commentaires

I understand. But the answer should be like this. Because it should be like,
6 - (2-1) = 5 (which will be inputted in cell D6)
5 - (2.5 - 2) = 4.5 (which will be inputted in cell D7)
4.5 - (3 - 2.5) = 4 (which will be inputted in cell D8)
Yutaka Yamada
Yutaka Yamada le 24 Mai 2021
Modifié(e) : Yutaka Yamada le 24 Mai 2021
Oh OK then, please try below.
N = length(Db1);
El1 = zeros(N,1);
El1(1) = Elev1;
%elevation
for i = 1:N-1
El1(i+1) = El1(i) - (Db1(i+1) - Da1(i+1));
end
I'll modify the answer also.
This worked, but I have a question, the last value for elevation returns the value #N/A
Could you try to copy and paste the above code again?
When I run the code, it becomes like below.
It worked. Thank you.

Connectez-vous pour commenter.

Plus de réponses (1)

In MATLAB the indices of array should be strictly positive integers. Zeros and negative numbers are not allowed.
This loop in your code:
for i = 0:100
El1(i) = Elev1(i-1) - (Db1 - Da1);
end
when i = 0, Elev1(-1) and El1(0) are not valid. Thats why you got error. Change the loop index so that the index is positive.

2 commentaires

What index should I put? I put i = 1:100 yet it did not work
KSSV
KSSV le 24 Mai 2021
Modifié(e) : KSSV le 24 Mai 2021
If you put i = 1:100, Elev1(0) you will get and this will be an error. By the way Elev1 looks to be a constant? Not an array. And also Db1 and Da1 are arrays...the logic is not correct.

Connectez-vous pour commenter.

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by