MATLAB: Is it possible to multiply elements in a matrix greater than a certain value without using a loop

loopsreplace elements

For example is it possible to find all the values greater than 1 in matrix 1 and multiply all the values greater than 1 by 2, so that you get something like matrix 2 without using a loop? matrix1 = [1 2 3; 4 5 6; 7 8 9] matrix2 = [1 4 6; 8 10 12; 14 16 18]

Best Answer

matrix1 = [1 2 3; 4 5 6; 7 8 9] ;
matrix2 = matrix1 ;
matrix2(matrix1>1) = matrix1(matrix1>1)*2 ;
matrix2 =
1 4 6
8 10 12
14 16 18