How to create time windows given start and stop indices
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Anas Khan
am 10 Nov. 2022
Kommentiert: Image Analyst
am 10 Nov. 2022
I have 2 vectors of indices: 1 for start times and 1 for end times:
>> size(trainStarts)
ans =
20 1
>> size(trainEnds)
ans =
20 1
Is there a way to, without using loops, that I can create a new variable representing windows that would be like trainStarts(1) : trainEnds(1), trainStarts(2) : trainEnds(2) and so on?
0 Kommentare
Akzeptierte Antwort
Voss
am 10 Nov. 2022
Here's one way:
trainStarts = [2 10 15];
trainEnds = [6 12 21];
result = arrayfun(@(x,y)x:y,trainStarts,trainEnds,'UniformOutput',false)
0 Kommentare
Weitere Antworten (1)
Image Analyst
am 10 Nov. 2022
Bearbeitet: Image Analyst
am 10 Nov. 2022
Yes, but why? What's wrong with having them in separate arrays.
for k = 1 : length(trainStarts)
thisRange = trainStarts(k) : trainEnds(k);
% Now do something with thisRange.
end
If you want to store/keep all the ranges separately you need to put them into cells because each range might be a different number of elements.
for k = 1 : length(trainStarts)
thisRange{k} = trainStarts(k) : trainEnds(k);
% Now do something with thisRange.
end
If you don't know what a cell array is see the FAQ:
2 Kommentare
Image Analyst
am 10 Nov. 2022
Then use my first snippet rather than my second snippet or the answer you Accepted. No sense storing stuff you don't need to store.
Siehe auch
Kategorien
Mehr zu Matrix Indexing 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!