How to write a function for a nxm matrix

8 vues (au cours des 30 derniers jours)
Violet
Violet le 19 Mai 2016
Commenté : Violet le 19 Mai 2016
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.

Réponse acceptée

dpb
dpb le 19 Mai 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.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by