Breaking and spliting vector into groups and averaging
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have a long one-dimensional time vector that I want to split in chunks and average each chunk. I have another logical vector (same length) of 0s and 1s that changes the sign to signal each chunk transition. How could I easily use the information on the logical vector to select each chunk of the time vector?
Thanks!
timevec = [0 2 3 4 5 0 9 3 4];
chunk = [0 0 0 1 1 1 1 0 0];
0 commentaires
Réponse acceptée
Adam Danz
le 10 Mar 2021
Modifié(e) : Adam Danz
le 10 Mar 2021
timevec = [0 2 3 4 5 0 9 3 4];
chunk = [0 0 0 1 1 1 1 0 0];
g = cumsum([1, diff(chunk)]~=0); % assuming 'chunk' is a row vector
groupMeans = splitapply(@mean, timevec, g)
5 commentaires
Adam Danz
le 11 Mar 2021
To define groups by the chunk vector resetting to 0,
chunk = [0 0 0 1 1 1 1 0 0 0 0 1]
g = cumsum(diff([1,chunk])==-1)
For the second question, do you mean this?
timevec = [0 2 3 4 5 0 9 3 4];
chunk = [0 0 0 1 1 1 1 0 0];
cond = logical([1 1 1 1 0 0 1 1 1]);
X = timevec(cond);
Y = chunk(cond);
g = cumsum([1, diff(Y)]~=0);
groupMeans = splitapply(@mean, X, g)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Histograms dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!