필터 지우기
필터 지우기

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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by