1

I am looking for a simple and fast way to get the index of an entry of a Time Series Object. For example:

%ts is a time series object with the following properties:
% Common Properties:
        Name: 'unnamed'
        Time: [70001x1 double]
    TimeInfo: [1x1 tsdata.timemetadata]
        Data: [70001x1 double]
    DataInfo: [1x1 tsdata.datametadata]

%first entry of ts is: 0 (time), 0.0667 (data)    
%second entry of ts is: 0.01 (time), 0.0667 (data)
%adn so on...

%I'm looking for an index i, such that i is the entry at the time 500.00
indexEntry = ts.time(i);
result = indexEntry == 500.00
%and resut should be true
d4rty
  • 3,970
  • 5
  • 34
  • 73
  • 1
    Please add some sample data in the exact (MATLAB syntax) format that you are using. Also, are you looking to match floating point values or integers because `==` is not reliable for finding floats... – Dan Nov 19 '15 at 11:17
  • I don't understand your example, using `indexEntry = ts.time(i);` you get the you use the value as an index: `ts.time(indexEntry)`. – Daniel Nov 19 '15 at 11:29
  • I just revised my question. I hope it is now more clearer. – d4rty Nov 19 '15 at 11:29
  • Not really, I don't get the meaning of `ts.time(ts.time(i))`, which is effectively what you have written. – Daniel Nov 19 '15 at 11:31
  • @Daniel That was a mistake. I just revised it. – d4rty Nov 19 '15 at 11:32

2 Answers2

1
tmp = 1:numel(ts.Time);
index = tmp(ts.time==500); %// 7000-by-1 logical array

The time at Time==500 can be found using logical indexing.

Following @Daniel's comment the following should be faster for single indices:

index = find(ts.time==500);
Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 2
    Instead of creating `tmp`, you could use `index = find(ts.time==500);` – Daniel Nov 19 '15 at 11:56
  • 1
    @Daniel Though it's more explicit, `find` does have more overhead than logical indexing. Most of the time it isn't really noticeable but worth considering depending on the application. – sco1 Nov 19 '15 at 12:09
  • @excaza of course the `tmp` uses up RAM, which might be a problem when trying to find a single number in an array of size `1e8` or so. Then the additional computational requirement of `find` might well be worth it. – Adriaan Nov 19 '15 at 12:21
  • 1
    @excaza: I compared both versions and `find` is never slower when you are searching for a single index. For large arrays, the `tmp` version is 20 times slower. – Daniel Nov 19 '15 at 12:40
0

Need to pay attention to the floating point data type.

index = find(abs(ts.time-500)<0.00001);
Tomato
  • 1
  • 2