sum function and add using loop

조회 수: 3 (최근 30일)
salahaldin
salahaldin 2013년 2월 25일
댓글: Jan 2016년 11월 30일
I have a column vector with 31536001 number of rows.
e.g. [34 ;30; 14; 12; 54; 46; 47; 48; 49; ......10000]';
How can I write a command using a FOR loop, that adds the first 3600 elements together the "purpose is to change from seconds to hour", then the second element from 3601 s to 7200 second and so on.my data in seconds i need it in hour for one year i have 31536001 s and i need my factor to be 8760 hours
d=[1;2;3;4;5;6;................]
i need
[x1,x2,........]
which has
x1=(1+2+....3600)
thanks
  댓글 수: 1
Jan
Jan 2013년 2월 26일
Please use meaningful tags, because they are used to classify the questions. "matlab" is not helpful, beause all questions in a Matlab forum concern this topic.
What do you want to do with the last chunk, which does not contain 3600 elements?

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

답변 (4개)

Jan
Jan 2013년 2월 26일
num = floor(length(d) / 3600);
result = zeros(1, num);
for k = 1:num
s = (k - 1) * 3600;
result(k) = sum(d(1+s:3600+s));
end
Or faster, but without FOR:
len = floor(length(d) / 3600) * 3600;
e = reshape(d(1:len), 3600, []);
result = sum(e);
  댓글 수: 3
NURULAIN OTHMAN
NURULAIN OTHMAN 2016년 11월 29일
i want to ask. i have some data from 1 to 24000. then, i grouped them 1 to 1000,1001 to 2000, 2001 to 3000 and so on. then, i want to sum up data 1 with data 1001 with data 2001.. how can i do? Can help me?
Jan
Jan 2016년 11월 30일
@NURULAIN OTHMAN: Please open a new thread for a new question. Then I'd post this answer:
x = rand(1, 24000);
result = sum(reshape(x, 1000, numel(x) / 1000), 1);

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


Honglei Chen
Honglei Chen 2013년 2월 25일
Here is an example that you have 10 elements and you add every two of them.
x = rand(10,1);
sum(reshape(x,2,[])).'

Miroslav Balda
Miroslav Balda 2013년 2월 26일
Let your data be in vector data(1:31536001). Try this code:
k = 0;
K = 0;
x = zeros(1,8760);
while k<8760,
k = k+1;
x(K) = sum(data(1+K:3600+K));
K = K+3600;
end

Andrei Bobrov
Andrei Bobrov 2013년 2월 26일
편집: Andrei Bobrov 2013년 2월 26일
data - your vector with size < 31536001 x 1 >;
n = numel(data);
[~,t] = histc(1:n,(0:3600:n) + eps(n));
out = accumarray(t(:),data(:));
or
out = sum(reshape([data;zeros(mod(-n,3600),1)],3600,[])).';

카테고리

Help CenterFile Exchange에서 Graphics Object Identification에 대해 자세히 알아보기

태그

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by