Use one function in another

조회 수: 3 (최근 30일)
Anastasiia Hanchevska
Anastasiia Hanchevska 2023년 3월 14일
댓글: Anastasiia Hanchevska 2023년 3월 14일
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
imran saeed
imran saeed 2023년 3월 14일
call second function from inside of first one.

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2023년 3월 14일
n = 7;
a = [3 4 1 2 5 2 1];
A = createMatrix(n, a)
A = 7×7
3 4 1 2 5 2 1 1 3 4 1 2 5 2 2 1 3 4 1 2 5 5 2 1 3 4 1 2 2 5 2 1 3 4 1 1 2 5 2 1 3 4 4 1 2 5 2 1 3
period = findPeriod(A)
period = 1
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
Dyuman Joshi
Dyuman Joshi 2023년 3월 14일
편집: Dyuman Joshi 2023년 3월 14일
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.
Anastasiia Hanchevska
Anastasiia Hanchevska 2023년 3월 14일
Yeah, i found the problem. Torsten helped so much. Thank all you guys

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by