sum every 5 rows of a column, Is the part of code right

I am trying to sum every 5 rows of a column, Is my part of code right
instdec = (instdec_t+instdec_d)/2;
startdec1 = (instdec >= 0.25*overalldec);
startdec2=zeros(npts,1);
while decpt==1
for j = 2:npts-4
startdec2(j) = sum(startdec1(j:j+4));
if startdec2(j)== 5
decpt=j-1;
break
end
end
I keep getting wrong answers, the second row becomes 2, and everything else is zeros
I would appreciate any support since I am very new to matlab

댓글 수: 2

I've applied a more readable indentation. Now it is obvious that an end is missing.
The purpose of your code is not clearly explained. All we have is the code you have shown here. Then it is impossible to guess, if it satisfies your needs. Please add a detailed description of the aim of the program - by editing the original question, not as comment and not as answer. Thanks.
can you provide some example data to test with?

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 6월 17일
편집: Andrei Bobrov 2013년 6월 17일
startdec2 = sum(reshape([startdec1;zeros(mod(-numel(startdec1),5),1)],5,[])).';
ADD
startdec1 = rand(42,1) < .7;
b = conv(startdec1,ones(5,1),'valid');
decpt = find(b == 5,1,'first')-1;

댓글 수: 5

Now you guess the initial value of decpt and npts?
I will try it out and let you know. thanks
I really appreciate you effort Andrei.
and yea jan, if I get you right. decpt 1 and npts is 42
but this way reduces my data as every 5 rows are summed as if I have 42 rows they will be reduced to 9
what I am looking for is to still have the full 42 rows where row 1 will be the sum of rows (1 to 5), row 2 will be sum of rows (2 to 6) and so on. till row 37 (sum of 37 till 42).
it is still bugging me, applying what i learnt from the book is giving me a headache.
please see ADD part in my answer.
Thanks I really appreciate it. I worked it out :)

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

추가 답변 (1개)

Hamaza24
Hamaza24 2013년 6월 17일

0 개 추천

Hi Jan and tom.
the purpose is to check if the numbers on every row exceed a certain value for a 5 successive rows.
so, the methodology I used is that if the condition of exceeding that certain number happens then generate 1, otherwise 0.
to check the of every five successive rows meet the condition, I want to sum every 5 rows and when the sum is 5 then I know that location and get its original value.
I can't really provide data tom. sorry for that and thanks for your help.

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by