Adding Number using for loop

29 visualizaciones (últimos 30 días)
Tehreem Syed
Tehreem Syed el 6 de Ag. de 2020
Comentada: Tehreem Syed el 6 de Ag. de 2020
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.

Respuesta aceptada

Benjamin Dassé
Benjamin Dassé el 6 de Ag. de 2020
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,

Más respuestas (1)

KSSV
KSSV el 6 de Ag. de 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
  2 comentarios
Tehreem Syed
Tehreem Syed el 6 de Ag. de 2020
Thanks, it was helpful.
Tehreem Syed
Tehreem Syed el 6 de Ag. de 2020
I can accept only one answer, all of the solutions were really helpful and correct for me.
Regards

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by