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 个评论

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:

请先登录,再进行评论。

回答(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.
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

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by