Effacer les filtres
Effacer les filtres

Saving variables from recursion?

2 vues (au cours des 30 derniers jours)
Michael Perry
Michael Perry le 7 Fév 2018
Modifié(e) : Stephen23 le 7 Fév 2018
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 commentaire
Stephen23
Stephen23 le 7 Fév 2018
Modifié(e) : Stephen23 le 7 Fév 2018
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.

Connectez-vous pour commenter.

Réponses (1)

ES
ES le 7 Fév 2018
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

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by