stopping the loop and save in a cell array

1 Ansicht (letzte 30 Tage)
HYZ
HYZ am 17 Mai 2020
Kommentiert: HYZ am 17 Mai 2020
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

Antworten (1)

KSSV
KSSV am 17 Mai 2020
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 Kommentar
HYZ
HYZ am 17 Mai 2020
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by