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 Kommentar

per isakson
per isakson am 2 Okt. 2017
Bearbeitet: per isakson am 2 Okt. 2017
  • If your file contains only numerical data, I think working with old time matrices rather than tables will make a significantly faster code.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Okt. 2017

0 Stimmen

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 Kommentare

Dileep Gudena
Dileep Gudena am 2 Okt. 2017
Thank you so much. There is an improvement in performance, but I was curious to know if it is possible to set an index in MATLAB.
Walter Roberson
Walter Roberson am 3 Okt. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by