Adding Number using for loop
Show older comments

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.
Accepted Answer
More Answers (1)
KSSV
on 6 Aug 2020
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
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!