Find indices of elements for given difference

Hi,
I have an incrementing time vector from 0 to 500 ms . Increment in time is not constant. I want to find indices every ~10 ms . E.g
t=[0, 1 ,3,4,7,10,13,15,16,19,20, 23,25,27,31...........500ms];
Then I would like to find indices of 10,20,31 ...., that will be 6th, 11th,15th.
Is this possible without loop.
thanks
jayant

1 Kommentar

If you know in advance which numbers you need to identify you can use find function.
Here there is a suggestion for finding multiple elements.
t=[0,1,3,4,7,10,13,15,16,19,20,23,25,27,31];
num = [10,20,31];
c = ismember(t,num);
indexes = find(c);

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

madhan ravi
madhan ravi am 19 Jul. 2020
Bearbeitet: madhan ravi am 19 Jul. 2020

0 Stimmen

Nearest element after or equal to the boundary:
Dt = t - (10:10:max(t)).';
Dt(Dt<0) = inf;
[~, Indices] = min(Dt,[],2)
Wanted = t(Indices)
Nearest elements before or equal it crosses boundary:
Dt = t - (10:10:max(t)).';
Dt(Dt>0) = -inf;
[~, Indices] = max(Dt,[],2)
Wanted = t(Indices)

1 Kommentar

Use
Dt = bsxfun(@minus, t, (10:10max(t)).') % if you’re using version prior to 2016b

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Bruno Luong
Bruno Luong am 19 Jul. 2020
Bearbeitet: Bruno Luong am 19 Jul. 2020

0 Stimmen

i = interp1(t, 1:length(t), 0:10:max(t), 'nearest', 'extrap');

9 Kommentare

madhan ravi
madhan ravi am 19 Jul. 2020
It’s wrong. This gives index one as well.
Bruno Luong
Bruno Luong am 19 Jul. 2020
Bearbeitet: Bruno Luong am 19 Jul. 2020
So why 1 is wrong? (assuming t starts from 0 as stated by OP)
madhan ravi
madhan ravi am 19 Jul. 2020
“I want to find indices every ~10 ms”
Bruno Luong
Bruno Luong am 19 Jul. 2020
Bearbeitet: Bruno Luong am 19 Jul. 2020
Every ~10 ms meaning (in english) is that the difference between 2 consecutive samples is about 10 ms, NOT necessary it must start at 10 ms.
madhan ravi
madhan ravi am 19 Jul. 2020
Bearbeitet: madhan ravi am 19 Jul. 2020
Thank you for the “English lessons” ;) I wonder why the OP didn’t include it in the expected result then xD.
Bruno Luong
Bruno Luong am 19 Jul. 2020
Bearbeitet: Bruno Luong am 19 Jul. 2020
We do not need OP to confirm whereas t=0 must be considered, since
>> why(0)
Barney wanted it that way.
Who are we to argue with Barney? Clear now?
madhan ravi
madhan ravi am 19 Jul. 2020
Who’s Barney? A God xD?
Here is the evidence
>> sum('Barney:')==sum('the god')
ans =
logical
1
madhan ravi
madhan ravi am 19 Jul. 2020
😂 , a good sense of humour after all.

Melden Sie sich an, um zu kommentieren.

dpb
dpb am 19 Jul. 2020

0 Stimmen

find and/or ismember will only return EXACT matches -- will NOT return something "on or about" a 10 ms interval.
Two possibilities come to mind
  1. ismembertol to find within some defined tolerance about the target, or
  2. interp1 with 'nearest' option
The second will return something for every input in range; the first may not find something if the spacing is such there isn't one within the given tolerance--or could potentially return more than one if the tolerance is too large.

1 Kommentar

dpb
dpb am 19 Jul. 2020
Possibly simply because 0 being first element wasn't hard to find... :)

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 19 Jul. 2020

0 Stimmen

Here's one way to record the index and time of when the times first cross "10" boundaries:
t = sort(randperm(500, 200)) % Sample data
times = [0,0];
counter = 1;
for k = 0 : 10 : max(t)
index = find(t >= k, 1, 'first'); % Find where it crosses multiple of 10 for the first time.
if ~isempty(index)
times(counter, 1) = index; % Log index
times(counter, 2) = t(index); % Log the actual time.
counter = counter + 1;
end
end
times % Show in command window.

1 Kommentar

Bruno Luong
Bruno Luong am 19 Jul. 2020
The same can be achieved without for-loop by using INTERP1 with 'NEXT' method in recent MATLAB realeases (just change 'nearest' in my anser to 'next'), or a combo of HISTC/ACCUMARRAY on older MATLAB.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by