How to write a function for a nxm matrix
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
dpb
on 19 May 2016
"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.
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!