Home > Back-end >  How to apply a condition on each item of a Matrix in MATLAB and get the same size of the input matri
How to apply a condition on each item of a Matrix in MATLAB and get the same size of the input matri

Time:11-11

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

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  

CodePudding user response:

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)
  • Related