Input multiple arrays into function
13 views (last 30 days)
Show older comments
Fredrik Scheie
on 2 May 2022
Commented: Fredrik Scheie
on 3 May 2022
I was wondering how to input several arrays into the function I have created? I am not very familiar with MATLAB. In python I would create a list of arrays and run the function in a for loop whereby appending the output to a new list. In MATLAB I understand it that the convention is to use cell arrays which can contain multiple arrays or other objects as I understand it? I tried running the following code, but I get an error. Here I want to input the matrices L2, L3 and get back a cell array of the corresponding output for each matrix.
L2 = [0 1/2 1/3 0 0 0 0;
1/3 0 0 0 1/2 0 0;
1/3 1/2 0 1 0 0 0;
1/3 0 1/3 0 1/2 0 0;
0 0 0 0 0 0 0;
0 0 1/3 0 0 1 0;
0 0 0 0 0 0 1];
L3 = [0,0,0,0,0;
1,0,0,0,0;
0,1,0,0,0;
0,0,1,0,0;
0,0,0,1,0];
d = sym('d');
Lists_mtxs = {L2, L3};
[Vs] = pagerank_function(Lists_mtxs,d);
Which calls the following function:
function [Vs] = pagerank_function(linkMatrix,d)
n = size(linkMatrix,1)
M = d * linkMatrix + (1-d)/n * ones(n)
% diagonal matrix eigenvalues D, eigenvectors mtx U
[U,D] = eig(vpa(M))
[d,ind] = sort(diag(D))
Ds = D(ind,ind)
Vs = V(:,ind)
end
2 Comments
Torsten
on 2 May 2022
How do you want to determine the zeros of a polynomial of order 7 where the coefficients depend on a symbolic variable ?
This command will not work:
[U,D] = eig(vpa(M))
And how do you want to sort symbolic roots ?
[d,ind] = sort(diag(D))
Accepted Answer
Catalytic
on 2 May 2022
Edited: Catalytic
on 2 May 2022
Simpler example,
L2 = [0 1/2 1/3 0 0 0 0;
1/3 0 0 0 1/2 0 0;
1/3 1/2 0 1 0 0 0;
1/3 0 1/3 0 1/2 0 0;
0 0 0 0 0 0 0;
0 0 1/3 0 0 1 0;
0 0 0 0 0 0 1];
L3 = [0,0,0,0,0;
1,0,0,0,0;
0,1,0,0,0;
0,0,1,0,0;
0,0,0,1,0];
fun=@(L) 3*L; %multiply both matrices by 3
V=cellfun(fun,{L2,L3},'uni',0);
V{:}
0 Comments
More Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox 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!