Clear Filters
Clear Filters

index must be a positive integer or logical.

1 view (last 30 days)
I am getting the following error while running the code:
Attempted to access (1,NaN); index must be a positive integer or
logical.
Error in
sh_row(i,(col+j-k(i)))=rgb(i,j);
% IF k is even right shift row else left shift row%%%%%%%%%%%%%%%%%
for i=1:1:row
for j=1:1:col
if(mod(k(i),2)==0)
if((j+k(i))<=col) %shift right of row
sh_row(i,j+k(i))=rgb(i,j);
row_shift_even(i,j)=j+k(i);
else
sh_row(i,(j+k(i)-col))=rgb(i,j); %HERE
row_shift_even(i,j)=(j+k(i)-col);
end
else
if((j-k(i))>=1) %shift left of row
sh_row(i,j-k(i))=rgb(i,j);
row_shift_odd(i,j)=j-k(i);
else
sh_row(i,(col+j-k(i)))=rgb(i,j);
row_shift_odd(i,j)=col+j-k(i);
end
end
end
end
  2 Comments
Walter Roberson
Walter Roberson on 22 Nov 2018
You had bolded the line
sh_row(i,(j+k(i)-col))=rgb(i,j);
as if it were the one the error is occuring on, but the error is occuring further down, at
sh_row(i,(col+j-k(i)))=rgb(i,j);
Walter Roberson
Walter Roberson on 22 Nov 2018
We have no information about the range of values in k(i). If k(i) were large then j-k(i) could be negative, and so would fail the j-k(i)>=1 test. If it were large enough, then col+j-k(i) could be negative or 0 in the line the problem is occuring on.

Sign in to comment.

Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!