Use one function in another
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Anastasiia Hanchevska
le 14 Mar 2023
Commenté : Anastasiia Hanchevska
le 14 Mar 2023
I have two functions, how can i use one in another ? I need to use my matrix A from first function in second one
function A = createMatrix(n, a)
n = 7
a = [3 4 1 2 5 2 1]
A = zeros(n);
for i = 1:n
for j = 1:n
A(i,j) = a(mod(j-i,n)+1);
end
end
end
There second
function period = findPeriod(A)
n = size(A, 1);
[~, idx] = max(A, [], 2);
idx = idx - 1;
g = n;
for k = 1:length(idx)-1
i = idx(k);
j = idx(k+1);
d = abs(i-j);
g = gcd(g, d / gcd(g, d));
end
period = g;
end
1 commentaire
Réponse acceptée
Torsten
le 14 Mar 2023
n = 7;
a = [3 4 1 2 5 2 1];
A = createMatrix(n, a)
period = findPeriod(A)
function A = createMatrix(n, a)
A = zeros(n);
for i = 1:n
for j = 1:n
A(i,j) = a(mod(j-i,n)+1);
end
end
end
function period = findPeriod(A)
n = size(A, 1);
[~, idx] = max(A, [], 2);
idx = idx - 1;
g = n;
for k = 1:length(idx)-1
i = idx(k);
j = idx(k+1);
d = abs(i-j);
g = gcd(g, d / gcd(g, d));
end
period = g;
end
3 commentaires
Dyuman Joshi
le 14 Mar 2023
Modifié(e) : Dyuman Joshi
le 14 Mar 2023
Are you sure you did exactly the same as what Torsten did in their answer?
What is the full error message? Copy-paste all of the red text.
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!