필터 지우기
필터 지우기

Nested Loop, Can someone help me to understand this code?

조회 수: 1 (최근 30일)
Kaouthar Bouyaouzan
Kaouthar Bouyaouzan 2021년 2월 3일
댓글: Kaouthar Bouyaouzan 2021년 2월 4일
Inspect the (basic) functionality of the cumsum function, then implement your own (mycumsum). Only implement the functionality of the first 3 documentation lines, not all the special cases!
function Y = mycumsum(X)
%MYCUMSUM cumulative sum of elements.
Y = X;
[r,c] = size(Y);
for ii = 2:r
for jj = 1:c
Y(ii,jj) = Y(ii,jj) + Y(ii-1,jj);
end
end
end
Can someone help me to understand this code?

채택된 답변

David Hill
David Hill 2021년 2월 3일
function Y = mycumsum(X)
Y = X;
[r,c] = size(Y); %determine size of maxtrix
for ii = 2:r %1st row will stay constant, start at 2nd row
for jj = 1:c %go through all columns
Y(ii,jj) = Y(ii,jj) + Y(ii-1,jj); %first step Y(2,1)=Y(2,1)+Y(1,1) then Y(3,1)=Y(3,1)+Y(2,1),... this is the cumulative add of column
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Configure and View Diagnostics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by