필터 지우기
필터 지우기

How can I calculate results from a function with two inputs using two arrays?

조회 수: 2 (최근 30일)
I have written a two-argument ackerman function as below. The code works and returns correct values if I specify the values or a variable (with a set value for the command line).
I need to test the function for all combinations of the arrays m=0:1:4 and n=0:1:3 (24 combinations). I can input an array using a for loop for m whilst setting n to 0 and this works fine and returns 6 answers for the cases (0,0) (1,0) (2,0) (3,0) (4,0) (5,0) (6,0). When I add the second array I only get 17 values rather than the 24 combinations.
How can I write this code so that it returns a list of all combinations (24)?
My command line input was below:
n=0:1:3;m=0:1:4;
for a=m
for b=n
res=ack(a,b)
end
end
function res=ack(m,n)
if m==0
res = n+1;
elseif m>0 && n==0
res = ack(m-1,1);
elseif m>0 && n>0
res = ack(m-1,ack(m,n-1));
end

채택된 답변

Mark McBroom
Mark McBroom 2018년 2월 25일
code looks correct. Does your code actually complete? Or is it simply taking a very long time to complete the 18th combination? Your function has very deep recursion and could therefore take a long time to compute whenever n > 0.
  댓글 수: 1
FortuitousMonkey
FortuitousMonkey 2018년 2월 25일
편집: FortuitousMonkey 2018년 2월 25일
Edit: The correct values I were required to use were m=0:1:3 and n=0:1:4.It now computes all of the values required. This is was computable. When m goes above 4 it appears not computable with my current code.
Thanks for the feedback.
I just checked after I left the computer for a while and a noticed appeared, so your guy sounds right.
Out of memory. The likely cause is an infinite recursion within the program.
Is there a way to compute these larger numbers in Matlab?

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

추가 답변 (1개)

Jan
Jan 2018년 2월 25일
편집: Jan 2018년 2월 25일
Something goes wrong.
I need to test the function for all combinations of the arrays m=0:1:4 and n=0:1:3
(24 combinations).
m has 5 elements, n has 4 elements, such that you get 20 combinations, not 24.
6 answers for the cases (0,0) (1,0) (2,0) (3,0) (4,0) (5,0) (6,0).
No, there is no 6. m goes from 0 to 4.
When I add the second array I only get 17 values rather than the 24 combinations.
Do you mean inputs or outputs?
As Mark M has said already: The function takes a long time for the input [4,1]. Currently it is still running on my computer...
[EDITED] ... still running - the values grow and grow
[EDITED 2] ... still running. I give up. Do you have an evidence, that the computations ends?
  댓글 수: 1
FortuitousMonkey
FortuitousMonkey 2018년 2월 25일
편집: FortuitousMonkey 2018년 2월 25일
Yep my mistake on the number of elements, just mixed up to different arrays.
My code failed eventually, I included the error message above.
Edit: Ackerman function always terminates. The recursion is bounded because in each recursive application either m decreases, or m remains the same and n decreases. (Wikipedia)

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by