A recursive matrix sum

1 次查看(过去 30 天)
Hey
I wrote a function that gives u the sum of the matrix. I try to write it with rerursive, but it doen't get well. i need an explanation..
this is my code -
function total=ff(l)
total = 0;
for i = 1:numel(l)
total = total+l(i);
end
any help?
  2 个评论
KSSV
KSSV 2020-10-21
Why recusrsion? It can be achieved with ease without recursion eh.
Noa  Yelin
Noa Yelin 2020-10-21
yes but I need to do it with recursion, Im learning it right now

请先登录,再进行评论。

采纳的回答

Asad (Mehrzad) Khoddam
You function is not recursive
function total=ff(l)
lp=l(:);
if numel(l) == 1
total = l(1);
else
total = lp(end) + ff(lp(1:end-1));
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by