Input multiple arrays into function

24 ビュー (過去 30 日間)
Fredrik Scheie
Fredrik Scheie 2022 年 5 月 2 日
コメント済み: Fredrik Scheie 2022 年 5 月 3 日
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 件のコメント
Torsten
Torsten 2022 年 5 月 2 日
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))
Fredrik Scheie
Fredrik Scheie 2022 年 5 月 3 日
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
Catalytic 2022 年 5 月 2 日
編集済み: Catalytic 2022 年 5 月 2 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by