Effacer les filtres
Effacer les filtres

Splitting vectors into cell array in a loop

1 vue (au cours des 30 derniers jours)
HYZ
HYZ le 12 Mai 2020
From the script below, I get fwd = [1 3 5 7 9 10 2 3 5 6 9 10 1 3 4 6 8 9 10];
I would like to save three separate vectors in a cell array as they are not continuous as shown in above picture.
The output I would like to get is fwd= {[1 3 5 7 9 10] ;[2 3 5 6 9 10] ;[1 3 4 6 8 9 10]}
Could anyone help me how to get the output?
Many thanks in advance.
a = [1 3 5 7 9 10 8 6 4 3 2 3 5 6 9 10 7 5 3 2 1 3 4 6 8 9 10 ];
m = length(a);
for i=2: m
if a(i)> a(i-1)
fwd(k) = a(i-1);
k=k+1;
end
end

Réponse acceptée

Stephen23
Stephen23 le 12 Mai 2020
>> a = [1,3,5,7,9,10,8,6,4,3,2,3,5,6,9,10,7,5,3,2,1,3,4,6,8,9,10];
>> d = [false,diff(a)>0,false];
>> B = find(diff(d)>0);
>> E = find(diff(d)<0);
>> C = arrayfun(@(b,e)a(b:e),B,E,'uni',0);
>> C{:}
ans =
1 3 5 7 9 10
ans =
2 3 5 6 9 10
ans =
1 3 4 6 8 9 10

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by