Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply a condition on each item of a Matrix in MATLAB and get the same size of the input matrix and calculate the number of ones?

I would like to set the values that are less than 0.0038 to one (1), and the rest of the values will be zeros (0), then I need to calculate the number of ones in the following matrix (berMap_mean) that has the size 41*41.

enter image description here

What I have done is:
First, I applied a condition that must show me a matrix with the same size that has 0 and 1 with the below condition

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

isSmaller = berMap_mean(berMap_mean < 0.0038);

isSmaller must be a matrix that has only zeros and ones of the same size of the above matrix due to the above condition as shown below.
enter image description here

Then I wanted to calculate the number of ones in the above-sketched matrix with the following condition.

numSmaller = sum(sum(isSmaller(:) == 1)); 

Then apply the following equation
Coverage_area = (numSmaller/(41*41))*400

But I couldn’t get the matrix that I wanted in the first condition but I got something different as shown below, and the second two following conditions are showing me nothing.

enter image description here

Any assistance, please? You can consider the berMap_mean any random map with the size of 41*41 double.

%% Calculate the coverage area

isSmaller = berMap_mean(berMap_mean < 0.0038);
numSmaller = sum(sum(isSmaller(:) == 1)); 
Coverage_area = (numSmaller/(41*41))*400  

>Solution :

Your expression for isSmaller looks right, that will give you a logical array the same size as berMap_mean. You can simplify the next expressions by using nnz to count the number of non-zero elements, and numel to count the number of elements.

fractionSmaller = nnz(isSmaller) / numel(isSmaller)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading