# Getting error as array indices must be positive integers or logical values in slope calculation.

1 visualizzazione (ultimi 30 giorni)
Ramesh Bala il 15 Set 2021
Commentato: Walter Roberson il 17 Set 2021
My R matrix is 512x43 and I would like to calculate slope by central diff from row wise values.
It shows logical values error ? What's the wrong in this slope calculation.
%testing slope by central difference
dy = zeros(43,1);
ddy = zeros(43,1);
delx = 1e-4;
for ij=1:1:512
for ji = 1:1:43
dy(ij,ji) = (R(ij,ji+1)-R(ij,ji-1))/(2/delx);
ddy(ij,ji) = (R(ij,ji+1)-2*R(ij,ji)+R(ij,ji-1))/delx^2;
end
end
Index in position 2 is invalid. Array indices must be positive integers or logical values.
##### 2 CommentiMostra NessunoNascondi Nessuno
Walter Roberson il 15 Set 2021
R(ij,ji-1)
What's that when ji is 1?
Ramesh Bala il 15 Set 2021
R(1,0) doesn't is nothing- doesnt exist

Accedi per commentare.

### Risposta accettata

Cris LaPierre il 15 Set 2021
Modificato: Cris LaPierre il 15 Set 2021
I suspect the error is in your column index for R:
• ji-1
When ji=1, the result is 0, which is not a valid index in MATLAB.
Note that you will have another problem when hi=43, as you also use ji+1. The result will be 44, which exceeds the size of R.
##### 5 CommentiMostra 3 commenti meno recentiNascondi 3 commenti meno recenti
Ramesh Bala il 15 Set 2021
Let's say if I stop it till 1 and then use another loop for rest? then join all together
for ji = 1
dy(ij,ji) = (R(ij,ji+1))/(2/delx);
for kj =2:1:42
dy2(ij,kj) = (R(ij,kj+1)-R(ij,kj-1))/(2/delx);
end
end
Walter Roberson il 17 Set 2021
You should be assigning to dy2(ij,1) and to dy2(ij,43)

Accedi per commentare.

### Più risposte (1)

Image Analyst il 15 Set 2021
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Image Processing Toolbox 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