Calculating the percentage in a matrix

I have a matrix
A=[1 2 1
2 2 3
1 1 2 ]
In this i want to calculate percentage for all values ,in this matrix i have three values 1,2,3
so the percentage must be
1-44.44%
2-44.44%
3-11.11%
please help

 채택된 답변

Honglei Chen
Honglei Chen 2012년 10월 17일
편집: Honglei Chen 2012년 10월 17일

0 개 추천

Here is one way to do it in MATLAB
A = [1 2 1;2 2 3;1 1 2];
[ua,~,uaidx] = unique(A(:));
uapercent = accumarray(uaidx,ones(numel(uaidx),1))/numel(uaidx);
[ua uapercent]
But if you have Statistics Toolbox, you can simply do
tabulate(A(:))

댓글 수: 1

[ua,~,uaidx] = unique(A(:));
upercent = histc(uaidx,ua)/length(uaidx)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2012년 10월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by