Home > other >  Looping through a matrix to identify numbers greater than or equal to 0.25 in matlab
Looping through a matrix to identify numbers greater than or equal to 0.25 in matlab

Time:02-22

I have created a 5x6 matrix of random numbers:

randn(5,6)

But next, I want to loop through the matrix and identify numbers that are greater than or equal to 0.25. I would like to create a new matrix where the corresponding entries are 1 if they are greater than 0.25 and 0 if they are less than 0.25. How would I do this?

CodePudding user response:

This code creates a logical matrix B with your desired result:

M = your matrix
B = M >= 0.25;

You can also use B in numeric calculations if needed.

CodePudding user response:

I think the easiest way to do this is as follows.

M = randn(5,6);
M(M>=.25) = 1;
M(M<.25) = 0;
  • Related