Splitting vectors into cell array in a loop
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Stephen23
on 12 May 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
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!