A recursive matrix sum

조회 수: 8 (최근 30일)
Noa  Yelin
Noa Yelin 2020년 10월 21일
댓글: Noa Yelin 2020년 10월 21일
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
Asad (Mehrzad) Khoddam 2020년 10월 21일
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
  댓글 수: 1
Noa  Yelin
Noa Yelin 2020년 10월 21일
thank you cery much

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by