Adding Number using for loop

조회 수: 28 (최근 30일)
Tehreem Syed
Tehreem Syed 2020년 8월 6일
댓글: Tehreem Syed 2020년 8월 6일
Hi,
I have 2 columns in data, time and s. I want to add all values of S when time = 1. Similarly, I want to repeat this for all values of time. I know this is a simple task, but i am not getting any output . Please help.

채택된 답변

Benjamin Dassé
Benjamin Dassé 2020년 8월 6일
Hi,
When time = 1 you can sum all values of S in writing :
sum(S(time==1))
So you can do for all time values
Results = zeros(numel(unique(time)),2)
j=0;
for i = unique(time)'
j=j+1;
Results(j,:) = [i sum(Y(time==i))] % [new_time new_S]
end
Regards,

추가 답변 (1개)

KSSV
KSSV 2020년 8월 6일
Let A be your n*2 array.
t = A(:,1) ;
s = A(:,2) ;
[C,ia,ib] = unique(t) ;
N = length(C) ;
iwant = zeros(N,1) ;
for i = 1:N
iwant(i) = sum(s(ib==i)) ;
end
iwant
  댓글 수: 2
Tehreem Syed
Tehreem Syed 2020년 8월 6일
Thanks, it was helpful.
Tehreem Syed
Tehreem Syed 2020년 8월 6일
I can accept only one answer, all of the solutions were really helpful and correct for me.
Regards

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by