MATLAB: Replacing elements in matrix with relational operations

matrixrelational operations

I am given a problem where I am given a matrix and I am suppose to change the values of the matrix depending on if a certain element of the matrix is greater than or less than a number. If it is greater than the variable n1 I am suppose to change the element to c1 and if is less than n1 I am suppose to change it to c2. I am to use only relational operations, I cannot use for, if, while or any implicit functions. How can I do this? This is what I have so far m3 is the matrix I am given
m4 = ((m3>n1)*c1)
m5 = ((m3<=n1)*c2)

Best Answer

M=randi(10,3);
n1 = 5;
c1 = 4;
c2 = 3;
M1 = M<5;
D1 = c1*M1;
M2 = M>=5;
D2 = c2*M2;
new_M = D1 + D2;
% In a single line
new_M = c1*(M<5) + c2*(M>=5);
Adapt the values of n1, c1 and c4 to yours needs.