A recursive matrix sum

8 Ansichten (letzte 30 Tage)
Noa  Yelin
Noa Yelin am 21 Okt. 2020
Kommentiert: Noa Yelin am 21 Okt. 2020
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 Kommentare
KSSV
KSSV am 21 Okt. 2020
Why recusrsion? It can be achieved with ease without recursion eh.
Noa  Yelin
Noa Yelin am 21 Okt. 2020
yes but I need to do it with recursion, Im learning it right now

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam am 21 Okt. 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by