what is the efficient way to search for value in a large struct

6 views (last 30 days)
shlomo odem
shlomo odem on 2 Mar 2022
Edited: Bruno Luong on 2 Mar 2022
i want to serch for value in spacific field ("Config") of very larg struct array (1e6 rows).
i do this serch all the time Therefore, I need the fastest way...
this is the way i try:
tree(1e6) = struct("Index",[],"parent", [],"Config",[],"Movment",[])
tree = 1×1000000 struct array with fields:
Index parent Config Movment
just for the exemple i feel the field Config with value...
[tree.Config] = deal(1);
becose of the size of this struc the serch is very slow.
i try diffrent metode like:
value2find = 15;
config = [tree.Config];
loc = ismember(config,value2find);
in this metod extruct all the value from the struct take long time:
timeit(@() [tree.Config])
ans = 0.2089
this way is the worst:
Loc = arrayfun(@(x) ismember(value2find,x.Config),tree);
timeit(@() arrayfun(@(x) ismember(value2find,x.Config),tree))
ans = 2.7032
I thought of a few ideas to deal with the problem, first I will detail the algorithm(RRT*) in a slightly abstract way:
I have a loop that produces random values ​​according to a certain regularity, each value is checked to see if it exists in the struct array. If the value exists, an update operation is performed for the same row in the struct array, if the value does not exist then it is added to the struct array in the first available space.
My thoughts:
  • Should I save the field I am looking for as a numeric array separately from the structure? So the search will be more efficient but will it require me to update both the array and the structure each time?
  • Maybe instead of a structure, I should use a cell array? Will it be significantly faster?
  • I will emphasize that the struct array is empty of values ​​at the beginning and fills with time, it may be worthwhile to limit the search to only the part that has values, of course as time goes by there will be more values ​​and therefore the search time will increase accordingly.
I would love for suggestions and better ways than I was able to do
Stephen23 on 2 Mar 2022
"Should I save the field I am looking for as a numeric array separately from the structure?"
That is probably the best option for your data.
But rather than inefficiently storing lots of separate scalar values in a large structure array, why not just use numeric arrays anyway (possibly in a scalar structure)? See Johan Pelloux-Prayer's answer.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 2 Mar 2022
Edited: Bruno Luong on 2 Mar 2022
You structure array seems to be linear, I would store the data in a structure of (cell) arrays rather than array of structures. This organization avoid to go back and forth to form the array of Config when searching.

More Answers (1)

Johan on 2 Mar 2022
I'm not sure that fits what you want but you could try making large arrays in a small structure instead of the opposite:
tree(1) = struct("Index",[],"parent", [],"Config",[],"Movment",[]);
tree.Config = rand(1e6,1);
tree2(1e6) = struct("Index",[],"parent", [],"Config",[],"Movment",[]);
value2find = 15;
for i = 1:100
loc = ismember(tree.Config,value2find);
timer(i,1) = toc;
loc = sum([tree2.Config]==value2find);
timer(i,2) = toc;
ans = 1×2
0.0008 0.2176

Community Treasure Hunt

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

Start Hunting!