How to write a function for a nxm matrix

8 ビュー (過去 30 日間)
Violet
Violet 2016 年 5 月 19 日
コメント済み: Violet 2016 年 5 月 19 日
I am trying to write a function that will divide rows by the sum of the column for any size matrix. I have code that works for a 3x3 but am struggling with writing a function to work for any size matrix (for example 500x500).
identity = eye(3);
a2 = xlsread('IO.xlsx', 1, 'B2:D2')
b2 = xlsread('IO.xlsx', 1, 'B3:D3')
c2 = xlsread('IO.xlsx', 1, 'B4:D4')
Xj = xlsread('IO.xlsx', 1, 'B6:D6')
xij = [a2; b2; c2]
format short
A1 = xij(1,:)/Xj(1)
A2 = xij(2,:)/Xj(2)
A3 = xij(3,:)/Xj(3)
A = [A1; A2; A3]
f = randi(200, 3, 1);
y = [inv(identity - A)]*f
Any guidance would be helpful thanks.

採用された回答

dpb
dpb 2016 年 5 月 19 日
"divide rows by the sum of the column for any size matrix."
x=bsxfun(@rdivide,x,sum(x));
bsxfun takes care of the singleton expansion for you automagically for such cases. The above does the computation in place; assign to a new variable if need to keep the old intact, of course.
  1 件のコメント
Violet
Violet 2016 年 5 月 19 日
Grazie mille!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by