Matlab里,怎么递归?

举个例子
2024-12-14 23:13:27
推荐回答(1个)
回答1:

就是自己调用自己,跟普通的递归道理相通。比如计算阶乘:% By lyqmath
function main()
clc; clear all; close all;
num = 6;
y = compute_prod(num)function y = compute_prod(num)
if num == 1
y = 1;
return;
else
y = num*compute_prod(num-1);
end
结果
y = 720>> prod(1:6)ans = 720>>