error in resultes of "OR" operator
    조회 수: 1 (최근 30일)
  
       이전 댓글 표시
    
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
채택된 답변
  the cyclist
      
      
 2013년 6월 29일
        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
추가 답변 (1개)
  Walter Roberson
      
      
 2013년 6월 29일
        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
참고 항목
카테고리
				Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


