Speed up search for matching strings
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
The following code works, but I would like to make if faster. Here, bowl_nodes and tmp_nodes are 1D arrays of the same size. Assume size(bowl_nodes) = size(tmp_nodes) = 1000. node_string is a string array. Each string in bowl_nodes matches with one and only one string in tmp_nodes. For example, the string in node_string( bowl_node(1) ) could be the same as the string in node_string( tmp_node(5) ). When matching strings are found, node_match stores the matching array indices and the element in tmp_nodes is removed. This way, every time the for-loop over m executes, it executes over fewer elements. When the code is done executing, tmp_nodes is empty. Can anyone recommend a way to speed up this code? The code's purpose is to fill up the node_match array, which links elements of bowl_nodes to elements of tmp_nodes and visa versa. (Note: tmp_nodes is a copy of a different array, so even though tmp_nodes gets removed by this code, the original array is still intact and is used later in the program.) Thanks.
for n= 1:size( bowl_nodes)
for m= 1:size( tmp_nodes)
if node_string( bowl_nodes(n) ) == node_string( tmp_nodes(m) )
node_match ( bowl_nodes(n) ) = tmp_nodes(m);
node_match ( tmp_nodes(m) ) = bowl_nodes(n);
tmp_nodes(m)= [];
break;
end
end
end
0 Commenti
Risposta accettata
dpb
il 5 Giu 2021
Only need one loop and I'd guess it'd be faster as written above if you didn't bother to remove the found elements--I suspect the memory reallocation is far more expensive than the extra looping/searching is--but I didn't try timing it to see.
The code snippet shown also doesn't preallocate the index array so it is being reallocated while going through the loop as well unless that is in the actual code but just not shown in the posting.
Try something like
node_match=arrayfun(@(s)find(matches(tmp_nodes,s)),bowl_nodes);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Data Type Identification in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!