Saving variables from recursion?

조회 수: 2 (최근 30일)
Michael Perry
Michael Perry 2018년 2월 7일
편집: Stephen23 2018년 2월 7일
So I've got this pretty basic fibonacci recursion:
function [F] = recursive_fibonacci(N)
if N < 1
N = 1;
end
if (N == 1) || (N == 2)
F = 1;
else
F = recursive_fibonacci(N - 1) + recursive_fibonacci(N - 2);
end
and I want to save each number outputted by the recursion (F) to a data file, in order. The question says to use the '–append' function. How would I do this?
  댓글 수: 1
Stephen23
Stephen23 2018년 2월 7일
편집: Stephen23 2018년 2월 7일
Note that save's '–append' option is misleadingly named: for .mat files it does NOT append data to the end of an existing variable, it will simply replace it entirely. For text files it does append to the end of the text file.

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

답변 (1개)

ES
ES 2018년 2월 7일
function [F] = recursive_fibonacci(N)
if N < 1
N = 1;
end
if (N == 1) || (N == 2)
F = 1;
else
F = recursive_fibonacci(N - 1) + recursive_fibonacci(N - 2);
save(filename,F,'-append')
end

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by