Input multiple arrays into function

조회 수: 25 (최근 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 CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by