How to write a function for a nxm matrix

8 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by