calculating the percentage and displaying

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

 채택된 답변

Image Analyst
Image Analyst 2012년 10월 5일

1 개 추천

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

kash
kash 2012년 10월 5일
ok thanks but i need as
62.5% same_value
37.5%others
i need to display same_values near 62.5% and others near 37.5%
Image Analyst
Image Analyst 2012년 10월 5일
편집: Image Analyst 2012년 10월 5일
fprintf('%.1f%%same_value\n\n', countsPercentage(1));
fprintf('%.1f%%others\n\n', countsPercentage(2));
fprintf('same_values near %.1f%%and others near %.1f%%\n\n', countsPercentage(1), countsPercentage(2));
kash
kash 2012년 10월 5일
i get as
62.5% same_value37.5%otherssame_values near 62.5% and others near 62.5%>> why do i get extra values as
same_values near 62.5% and others near 62.5%>>
Image Analyst
Image Analyst 2012년 10월 5일
편집: Image Analyst 2012년 10월 5일
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 2019년 5월 15일
편집: nor azam 2019년 5월 15일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2012년 10월 5일

편집:

2019년 5월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by