i want to replace 2 pixels in the matrix by using this function between two pixels throw me this error

for k=1:ch2
for i=1:r2
for j=1:c2
if New_im(i,j,k) >New_im(i+3,j,k)
New_im(i+1,j,k)=New_im(round((((New_im(i,j,k) - New_im(i+3,j+3,k))/fact)*2) + New_im(i,j,k)),j,k) ;
New_im(i+2,j,k)=New_im(round((((New_im(i,j,k) - New_im(i+3,j+3,k))/fact)*1) + New_im(i,j,k)),j,k) ;
end
end
end
end
error that throwd
Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 Comment

What is the value of i, i+2, and i+3 when it throws the error? Are any of them floating point (have fractional parts), zero, negative, or larger than the number of rows in the New_im array?
What are the values of ch2, r2, c2, and fact?
You can reply after you read this:

Sign in to comment.

Answers (2)

Use the debugger to examine the problem:
dbstop if error
Then run the code again. When Matlab stops in the failing line, check the locally used variables. I guess that this is the line:
New_im(i+1,j,k)=New_im(round((((New_im(i,j,k) - New_im(i+3,j+3,k))/fact)*2) + New_im(i,j,k)),j,k);
Then check in the command window:
i, j, k
New_im(i,j,k)
(((New_im(i, j, k) - New_im(i+3, j+3, k)) / fact) * 2
round((((New_im(i, j, k) - New_im(i+3, j+3, k)) / fact) * 2) + New_im(i,j,k))
Obviously the index is not a positive integer.

1 Comment

Note in particular that the result of the round() + New_img(i,j,k) is being used as a index into New_im

Sign in to comment.

Is this what you mean to do?
for k=1:ch2
for i=1:r2
for j=1:c2
if New_im(i,j,k) > New_im(i+3,j,k)
diff_im = (New_im(i,j,k) - New_im(i+3,j+3,k))/fact;
New_im(i+1,j,k) = round(diff_im*2 + New_im(i,j,k));
New_im(i+2,j,k) = round(diff_im + New_im(i,j,k));
end
end
end
end

Categories

Asked:

on 15 Mar 2022

Commented:

on 15 Mar 2022

Community Treasure Hunt

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

Start Hunting!