Splitting vectors into cell array in a loop

1 回表示 (過去 30 日間)
HYZ
HYZ 2020 年 5 月 12 日
回答済み: Stephen23 2020 年 5 月 12 日
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

採用された回答

Stephen23
Stephen23 2020 年 5 月 12 日
>> 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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by