How to normalize a matrix such that each column sums equal 1

32 ビュー (過去 30 日間)
Clarisha Nijman
Clarisha Nijman 2018 年 10 月 4 日
コメント済み: Clarisha Nijman 2018 年 10 月 4 日
Hello,
I have a matrix A=[1 2 3; 1 3 6] and want to normalize the matrix such that each column sum equals 1.
The normalized matrix should be: P=[0.5 0.4 0.33; 0.5 0.6 0.67]
I tried these codes:
P=normc(A);
P=(A)/sum(A,1);
P=A/norm(A,1);
but they do not result in a matrix with column sums 1.
Can somebody give me some feedback?
Thank you in advance,
Clarisha

採用された回答

dpb
dpb 2018 年 10 月 4 日
You're missing the "dot" operator so the division is matrix divide not element-wise. See
doc ./
doc rdivide % that's a stretch to know the function name, for sure... :)
A=A./sum(A);
Look up how Matlab operates on arrays in documentation for sum to see why this works for what you want with only one argument...

その他の回答 (1 件)

Jim Riggs
Jim Riggs 2018 年 10 月 4 日
編集済み: Jim Riggs 2018 年 10 月 4 日
[row, col] = size(A);
for i=1:col
P(:,i)= A(:,i)/sum(A(:,i))
end
  1 件のコメント
Clarisha Nijman
Clarisha Nijman 2018 年 10 月 4 日
Thanks a lot, this also works! Only a semicolon at the end of the third line.

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

カテゴリ

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