# Input multiple arrays into function

8 views (last 30 days)
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:
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 CommentsShowHide 1 older comment
Fredrik Scheie on 3 May 2022
Yes, the point here is to observe how the damping factor "d" here influences the calculated eigenvectors which I want to sort from smallest to largest. In that case its ok to end up with expressions for the eigenvectors where "d" is treated as a symbolic variable if I am understanding your question correctly?
[U,D] = eig(vpa(M)) - I used this because I wanted to normalize the eigenvectors in symbolic form. What should I use instead?
In terms of sorting the symbolic roots [d,ind] = sort(diag(D)) its really the eigenvectors which are important and the ones I want to return as an output, but I need to sort the eigenvalues such that they correspond with eigenvectors sorted from smallest to largest. Or do you mean sorting in terms of getting complex solutions when solving the characteristic polynomial? Not sure I understand.

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{:}
ans = 7×7
0 1.5000 1.0000 0 0 0 0 1.0000 0 0 0 1.5000 0 0 1.0000 1.5000 0 3.0000 0 0 0 1.0000 0 1.0000 0 1.5000 0 0 0 0 0 0 0 0 0 0 0 1.0000 0 0 3.0000 0 0 0 0 0 0 0 3.0000
ans = 5×5
0 0 0 0 0 3 0 0 0 0 0 3 0 0 0 0 0 3 0 0 0 0 0 3 0

R2021b

### Community Treasure Hunt

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

Start Hunting!