MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Aseel H on 29 Jun 2013

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

*No products are associated with this question.*

Answer by the cyclist on 29 Jun 2013

Accepted answer

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;

Answer by 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