How to write a function for a nxm matrix

8 Ansichten (letzte 30 Tage)
Violet
Violet am 19 Mai 2016
Kommentiert: Violet am 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.

Akzeptierte Antwort

dpb
dpb am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by