Calculating the percentage in a matrix

4 views (last 30 days)
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

Accepted Answer

Honglei Chen
Honglei Chen on 17 Oct 2012
Edited: Honglei Chen on 17 Oct 2012
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 Comment
Matt Fig
Matt Fig on 17 Oct 2012
[ua,~,uaidx] = unique(A(:));
upercent = histc(uaidx,ua)/length(uaidx)

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!