Find intervals in a vector
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
René Lampert
am 10 Feb. 2021
Beantwortet: David Hill
am 10 Feb. 2021
Lets suppose we have the vector A=[0 5 7 8 0 0 0 1 9 6 0 0 1 2]. The goal is to extract the indices of the different intervals inside the vector A and write it in seperate vectors. With intervals I mean the nonzero numbers. So at the end of the code I want the vectors B=(2 3 4) C=(8 9 10) D=(13 14), where the entries are the indices of the intervals (consecutive nonzero numbers) in A.
Is there a possibility to accomplish this task in a general way ?
Thanks
0 Kommentare
Akzeptierte Antwort
David Hill
am 10 Feb. 2021
a=find(A~=0);
b=[0,find(diff(a)>1),length(a)];
for k=2:length(b)
c{k-1}=a(b(k-1)+1):a(b(k));
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical 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!