stopping the loop and save in a cell array

조회 수: 1 (최근 30일)
HYZ
HYZ 2020년 5월 17일
댓글: HYZ 2020년 5월 17일
Hi,
I have a vector:
a = [1 3 4 2 3 4 5 6 3 4 5 6 8 9 10 9 8 7 6 7 8 9 8 7 5 4 3 2 4 5 3 1 2 4 4 3 2 4 6 5 4 6 7 9 9 10 9 8 7 6 7 8 9 8 7 5 4 3 2 4 5 3 1];
The output I would like to have is one cell: {[3 4 2 3 4 5 6 3 4 5 6 8]; [4 4 3 2 4 6 5 4 6 7]} as underlined in the original vector. The code I have written is shown below. I am stuck with getting a very long vector for the code I wrote.
Could anyone please advise how to correct the code? Thanks a lot!
The purpose is to split general forward and back directions. The values are like positions. for example, I am aware that there are backward tracks in the desired output [3 4 2 3 4 5 6 3 4 5 6 8]. I would like to ignore first. 1,2,9 and 10 are positions at the extremes which are excluded as min(a)+2 and max(a) -2.
a = [1 3 4 2 3 4 5 6 3 4 5 6 8 9 10 9 8 7 6 7 8 9 8 7 5 4 3 2 4 5 3 1 2 4 4 3 2 4 6 5 4 6 7 9 9 10 9 8 7 6 7 8 9 8 7 5 4 3 2 4 5 3 1 ];
start = min(a)+2;
ed = max(a) -2;
m = length(a);
f = 1;
i = 2; k=1;
while i < m
if a(i) >= start && a(i-1) <= start && a(i+1) >= a(i)
fwd1(k) = a(i);
for j = i+1:m-1
if a(j+1) >= ed && a(j+2) >= a(j+1) && a(j) <= a(j+1)
fwd2(k) = a(j);
output{k} = a (i:j);
end
end
i=j+1;
else
i = i+1;
end
k= k+1;
end

답변 (1개)

KSSV
KSSV 2020년 5월 17일
Follow somehting likethis:
a = [1 3 4 2 3 4 5 6 3 4 5 6 8 9 10 9 8 7 6 7 8 9 8 7 5 4 3 2 4 5 3 1 2 4 4 3 2 4 6 5 4 6 7 9 9 10 9 8 7 6 7 8 9 8 7 5 4 3 2 4 5 3 1];
[val0,id0] = min(a) ;
[val1,id1] = max(a) ;
iwant1 = a(id0+1:id1-2) ;
a(id0:id1) = [] ;
% second time
[val0,id0] = min(a) ;
[val1,id1] = max(a) ;
iwant2 = a(id0+1:id1-2) ;
a(id0:id1) = [] ;
  댓글 수: 1
HYZ
HYZ 2020년 5월 17일
Sorry ... the vector I gave was an example. The index of where the output vectors start will not be the same always. It would be good to use loop to check each position by each.
If possible, could you help me advise using loop as I mentioned as above? Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by