Input multiple arrays into function
13 次查看(过去 30 天)
显示 更早的评论
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
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))
采纳的回答
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{:}
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!