Need help writing function taking matrix as input (linear algebra/matlab)
Show older comments
This is only one part of a larger assignment, but I hope I can make it clear. The problem is as follows:
Write a matlab function ranking(A) which returns a score vector x (basically means solve Mx=x) for the nxn matrix M defined by:
M=(1-m)A+mS
when the input matrix A is a nxn stochastic (means that all entries are non-negative and the sum of all columns add up to one) link-matrix (in our hypothetical web no webpages can have link to themselves) where m=0.15 and S is a nxn matrix where all the entries are 1/n
If A is non-stochastic, the function should return an error message
If this didn't make any sense, hopefully you will get the idea when you see some of my scribbling:
function x = ranking(A)
n=size(A,1);
for k=1:n
s=sum(A(:,k))
if s~=1
fprintf('Input must be a stochastic matrix' );
end
if (A(k,k)~=0)
fprintf('Input must be a stochastic matrix' );
end
for e=1:n
if A(e,k)<0
fprintf('Input must be a stochastic matrix' );
end
if A(e,k)>1
fprintf('Input must be a stochastic matrix' );
end
end
end
S=ones(n)*(1/n);
M=(0.85)*A+(0.15*S);
x=rref(M)
This is just some idea of how it might look like. I couldn't figure out how to make a function take a matrix as input
If anyone will care to help me out I will be grateful
1 Comment
James Tursa
on 20 Sep 2016
Edited: James Tursa
on 20 Sep 2016
I don't understand. The argument A can be anything you want, including a matrix. Just pass a matrix as an argument and it will be a matrix inside the function.
Also, some of your comparison tests could be problematic due to floating point inaccuracies. E.g.,
s=sum(A(:,k))
if s~=1
It could be that the sum gets calculated as very close to, but not exactly equal to, 1. So this test could be coded better using a tolerance.
Answers (0)
Categories
Find more on Matrix Indexing 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!