# What's the problem in my loop?

2 visualizzazioni (ultimi 30 giorni)
Febin Benjamin il 30 Ago 2013
B=zeros(255,255);
for j=0:255
for k=0:255
B(j,k)=A(j,k+1)-A(j,k-1);
end
end
figure,imshow(B);
error: Attempted to access A(0,1); index must be a positive integer or logical.
Error in tryfebin (line 6)
B(j,k)=A(j,k+1)-A(j,k-1);
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Jan il 30 Ago 2013
The error message is very meaningful: "index must be a positive integer or logical"

Accedi per commentare.

### Risposta accettata

David Sanchez il 30 Ago 2013
Your code should go like this:
B=zeros( size(A) );
for j=1:size(A,1)
for k=2:(size(A)-1) %edge pixels of B matrix has to be treated differently
B(j,k)=A(j,k+1)-A(j,k-1); %you can not apply this algorithm to edge pixels
end
end
figure,imshow(B);
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (1)

ES il 30 Ago 2013
Matlab Matrices do not have indices starting at 0. There is nothing like A(0) in MATLAB (But it is so in C or Python though). This is a common issue faced by people migrating from C or Python to MATLAB.
Solution: Your for loop with j should run from 1 to 256.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Point Cloud Processing 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