필터 지우기
필터 지우기

I have a list of 120 elements in a column, and I need to sum each 12 elements and then sum together the next 12 so from the 120 values I will have 12 different sum values.

조회 수: 3 (최근 30일)
I just need a for loop to get the sum of each 12 elements in the column for 10 consequative times.

답변 (2개)

KSSV
KSSV 2020년 2월 10일
편집: KSSV 2020년 2월 10일
YOu need not to use a loop for this...... reshape your column and use sum.
c = rand(120,1) ; % your column matrix
iwant = sum(reshape(12,[])) ;

hari
hari 2020년 2월 10일
sum = 0;
temp = 0;
totalSum = [];
for i = 1:length(c)
sum = sum+c(i);
temp = temp+1;
if (temp == 12)
ttalSum = [totalSum sum];
sum = 0;
temp = 0;
end
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 10일
We firmly recommend that you never use sum as the name of a variable, as it is very common to also need to use sum() as a function call.
This is especially important when you are using scripts, because that variable named sum is going to be left in the workspace and you are going to get very confused when you have forgotten it and try to use sum() function.
KSSV
KSSV 2020년 2월 10일
Type error....
ttalSum = [totalSum sum];
shoule be replaced to :
totalSum = [totalSum sum];

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

카테고리

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

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by