How can I split measured data in separate vectors, depending on sign value?

8 vues (au cours des 30 derniers jours)
Hello! Is it possible to split measured data f.e.v= [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1] in separate vectors, f.e. only the 'growing' values: v1=[1 2 3 4 5], v2=[1 2 3 4 5].
Maybe with a find(sign(diff(v))==1) ?
Thank you!

Réponse acceptée

Star Strider
Star Strider le 7 Mar 2022
One approach —
v= [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1];
dv = gradient(v);
sel = v(dv>=0);
dsel = diff([1 find(diff([sel(1) sel])<0) numel(sel)+1]);
C = mat2cell(sel, size(sel,1), dsel)
C = 1×2 cell array
{[1 2 3 4 5]} {[1 2 3 4 5]}
v1 = C{1}
v1 = 1×5
1 2 3 4 5
v2 = C{2}
v2 = 1×5
1 2 3 4 5
.
  2 commentaires
Anna B.
Anna B. le 7 Mar 2022
Modifié(e) : Anna B. le 7 Mar 2022
Thank you very much for the quiсk response. I have tried both variants and it works perfectly!
Star Strider
Star Strider le 7 Mar 2022
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 7 Mar 2022
Modifié(e) : Jan le 7 Mar 2022
Start with a simple loop approach:
v = [1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 1];
dv = [false, diff(v) > 0, false];
ini = strfind(dv, [false, true]);
fin = strfind(dv, [true, false]);
C = cell(1, numel(ini));
for iC = 1:numel(ini)
C{iC} = v(ini(iC):fin(iC));
end
C
C = 1×2 cell array
{[1 2 3 4 5]} {[1 2 3 4 5]}
  1 commentaire
Anna B.
Anna B. le 7 Mar 2022
Modifié(e) : Anna B. le 7 Mar 2022
Thank you very much for the quick response. I have tried both variants and it works perfectly!

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by