- If your file contains only numerical data, I think working with old time matrices rather than tables will make a significantly faster code.
How to set an index on a table
17 views (last 30 days)
Show older comments
Dileep Gudena
on 2 Oct 2017
Commented: Walter Roberson
on 3 Oct 2017
I have an csv file and am reading it using readtable(). I want to set a index on this table for fast access of data (something similar to set_index in python(pandas)).
T=readtable('file.csv');
index1=find(T.column1==value1);
index2=find(T.column2==value2);
index=intersect(index1,index2);
sam=table2array(T(index,6));
I want to do this for different values of value1 and value2 (approx 12000 times). This is taking about 600 seconds for 12000 records. Is there a way to access the table fast?
Thanks in advance.
1 Comment
per isakson
on 2 Oct 2017
Edited: per isakson
on 2 Oct 2017
Accepted Answer
Walter Roberson
on 2 Oct 2017
MATLAB tables are not database objects, and do not have indexes like are created with pandas set_index.
You could improve performance by using
T=readtable('file.csv');
mask = T.column1 == value1 & T.column2 == value2;
sam = T{mask,6};
If you had a number of these to do then you could:
need_to_match = [value_1s_to_match(:), value_2s_to_match(:)];
[tf, idx] = ismember(T{:,1:2}, need_to_match);
Now all of the places that idx are 1 belong to the first array, 2 belong to the second case, and so on; idx will be 0 for rows that do not match any of the cases.
But before making a recommendation on how to best use the index, I would ask you to have a look at https://www.mathworks.com/help/matlab/ref/findgroups.html#buxwasl and say whether that looks like what you are trying to do.
2 Comments
Walter Roberson
on 3 Oct 2017
No, there is not.
containers.Map which uses a hash table method for lookups, but that probably would not help in this situation.
Perhaps for your purposes using a kd-tree would be appropriate. https://www.mathworks.com/help/stats/kdtreesearcher.html
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!