How to delete consecutive values in a vector and to save just the biggest one?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Miro Mitev
am 1 Sep. 2016
Kommentiert: Stephen23
am 19 Sep. 2019
I have a vector like: a=[1 2 3 7 10 11 12]. The idea that i want to implement is: if I have consecutive numbers (in this situation) 1 2 3 and 10 11 12 to save just the biggest one and still to not delete the "7". So it should become: a=[3 7 12].
My code (it doesnt work properly and gives me error also):
a=[1 2 3 7 10 11 12]
k=numel(a);
for n=2:k
if a(n)==(a(n-1)+1)
a(n)=[];
end
end
0 Kommentare
Akzeptierte Antwort
Stephen23
am 1 Sep. 2016
Bearbeitet: Stephen23
am 1 Sep. 2016
If the sequences are integer and increasing only:
>> a = [1,2,3,7,10,11,12];
>> x = [diff(a)~=1,true];
>> a(x)
ans =
3 7 12
3 Kommentare
Brian
am 18 Sep. 2019
If anyone comes across this and is having issues ("horzcat" error) using vertically orientied data as I was, just transpose the array inside of the diff function. For example:
>> a = [1;2;3;7;10;11;12]
>> x = [diff(a')~=1,true];
>> a(x)
ans =
3
7
12
Stephen23
am 19 Sep. 2019
>> x = [diff(a)~=1;true];
>> a(x)
ans =
3
7
12
Weitere Antworten (0)
Siehe auch
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!