Splitting vectors into cell array in a loop

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Stephen23
Stephen23 il 12 Mag 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by