4 views (last 30 days)

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

madhan ravi
on 19 Jul 2020

Edited: madhan ravi
on 19 Jul 2020

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)

madhan ravi
on 19 Jul 2020

Use

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

Bruno Luong
on 19 Jul 2020

Edited: Bruno Luong
on 19 Jul 2020

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

dpb
on 19 Jul 2020

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

- ismembertol to find within some defined tolerance about the target, or
- 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.

Image Analyst
on 19 Jul 2020

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.

Bruno Luong
on 19 Jul 2020

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/567459-find-indices-of-elements-for-given-difference#comment_942396

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/567459-find-indices-of-elements-for-given-difference#comment_942396

Sign in to comment.