calculating the percentage and displaying

9 views (last 30 days)
I have a 256*256 matrix consisting of ones and two's for example i have a 4x4matrix
A=[1 2 2 1
1 2 1 1
2 2 2 1
1 1 1 1 ]
in the A matrix i want to calculate the percentage of ones and two's
i have 10 number of 1's (10/16*100) so i want to display as
62.5% same_value
37.5%others
please help

Accepted Answer

Image Analyst
Image Analyst on 5 Oct 2012
Try this:
A=[1 2 2 1
1 2 1 1
2 2 2 1
1 1 1 1 ]
numberOfBins = max(A(:));
countsPercentage = 100 * hist(A(:), numberOfBins) / numel(A)
In the command window:
A =
1 2 2 1
1 2 1 1
2 2 2 1
1 1 1 1
countsPercentage =
62.5 37.5
  5 Comments
Image Analyst
Image Analyst on 5 Oct 2012
Edited: Image Analyst on 5 Oct 2012
I printed out to the command window exactly what you said you need: "i need as...." and "i need to display...."
I edited the above code in the above comment to add the \n to put them on separate lines. Try again.
nor azam
nor azam on 15 May 2019
Edited: nor azam on 15 May 2019
Hi Image Analyst, what if i want to print the value along with the image. Means that i used imshow to show the image and i want to print the percentage on the image. Is there any other way? please help me.
Here is my code after binarize.
BW2 = imbinarize(rightHalf)
figure;
imshowpair(rightHalf,BW2,'montage')
percentageBlackRight=(1-nnz(rightHalf)/numel(rightHalf))*100

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!