error in resultes of "OR" operator
2 views (last 30 days)
Show older comments
in the following code I need subtract the values 255 and 254 by 1
and adding the value 0 and 1 by 1 and the other values don't change
X=[255 250 0 100 38 47 150 254
253 251 250 0 0 47 25 252
0 0 0 90 145 74 30 249
200 220 0 1 2 3 225 4
0 255 251 0 4 20 35 245
255 250 0 100 38 47 150 254
200 220 0 1 2 3 225 4
253 251 250 0 0 47 25 252];
Max = 255;
Min = 0;
[x,y] = size(X);
for i= 1:x
for j = 1:y
if X(i,j)==(Max | Max-1)
X1(i,j) = X(i,j)-1;
elseif X(i,j)== (Min | Min+1)
X1(i,j) = X(i,j)+1;
else
X1(i,j)= X(i,j);
end
end
end
**************************************
but resultes not change:it give me
X1=[255 250 0 100 38 47 150 254
253 251 250 0 0 47 25 252
0 0 0 90 145 74 30 249
200 220 0 1 2 3 225 4
0 255 251 0 4 20 35 245
255 250 0 100 38 47 150 254
200 220 0 1 2 3 225 4
253 251 250 0 0 47 25 252];
Please....I need know what error in the code
0 Comments
Accepted Answer
the cyclist
on 29 Jun 2013
You have a syntax problem when you try to do
X(i,j)==(Max | Max-1)
because that is not going to check whether X(i,j) is in a set. That is valid MATLAB syntax, but it is not doing what you think it is. You might want to read the documentation for the or() function to see exactly what it does:
doc or
Instead, I think you want the syntax
if (X(i,j)==Max) | (X(i,j)==(Max-1))
You could also use
if ismember(X(i,j),[Max Max-1])
Finally, note that you can do this operation on the entire matrix at once:
X1 = X;
idxHi = ismember(X,[255,254])
X1(idxHi) = X(idxHi)-1;
idxLo = ismember(X,[0,1])
X1(idxLo) = X(idxLo)+1;
0 Comments
More Answers (1)
Walter Roberson
on 29 Jun 2013
X(i,j)== (Min | Min+1)
means to evaluate the logical condition (Min | Min+1) and then compare that logical result to what is in X(i,j).
Try
if X(i,j) == Min | X(i,j) == Min+1
0 Comments
See Also
Categories
Find more on Multidimensional Arrays 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!