Changing a matrix into a binary matrix

2 views (last 30 days)
Alex Spratt
Alex Spratt on 3 Feb 2016
Edited: Star Strider on 3 Feb 2016
I have a large matrix in which i need to turn it into a binary matrix form. There are criteria in which each value needs to meet. If a value in the matrix is greater than the second deviation of the matrix it is assigned a 1, and if the value is less than or equal to the standard deviation it shall be given a value of 0. Does anybody know how to translate the if statement around the matrix and then change the value to either a 1 or 0. Any replies would be grateful.

Answers (1)

Star Strider
Star Strider on 3 Feb 2016
Edited: Star Strider on 3 Feb 2016
This is how I would do it:
M = randn(10); % Matrix
M0 = M - mean(M(:)); % Subtract Mean
Mstd = std(M0(:)); % Standard Deviation of ‘M’
Mgt = M0 > Mstd; % Greater Than 1 S.D.
If you want it to be symmetrical, that is to set all values either >1 S.D. or <-1 S.D. to 1, do this:
M = randn(10) + 1; % Matrix
M0 = M - mean(M(:)); % Subtract Mean
Mstd = std(M0(:)); % Standard Deviation of ‘M’
Mgt = (M0 > Mstd) | (M0 < -Mstd); % Greater Than 1 S.D. -OR- Less Than -1 S.D.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!