Calculating the percentage in a matrix

5 ビュー (過去 30 日間)
kash
kash 2012 年 10 月 17 日
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 日
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 件のコメント
Matt Fig
Matt Fig 2012 年 10 月 17 日
[ua,~,uaidx] = unique(A(:));
upercent = histc(uaidx,ua)/length(uaidx)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by