A very fast way to find elements and their indices? (Is ismember fast?)
    18 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
A very fast way to find elements and their indices? (Is ismember fast?)
This would be my example:
% input: create arrays "a" and "b"
a = {'A23'};
l = 'A' : 'Z';
rl = reshape(l,size(l,2),1);
for i = 1 : size(rl,1)
    for j = 1 : 10000
           f{i,j} = sprintf('%s%d',rl(i), j);
    end
end
b = reshape(f,[],1);
% still part of the input: randomly replace 50 values of "b" with {'A23'}
r = round((size(b,1)-1) .* rand(50,1) + 1);
b(r) = a;
% How can I make this part way much faster ?
tic
[~,idx2] = ismember(string(b),string(a));
b_filtered = b(find(idx2),:);
toc
4 Commenti
  Steven Lord
    
      
 il 18 Nov 2022
				If you're trying to generate random integer values in an interval, use randi instead of rand.
Risposta accettata
  Bruno Luong
      
      
 il 18 Nov 2022
        
      Modificato: Bruno Luong
      
      
 il 18 Nov 2022
  
      You overly complicate your code for nothing, and yes ismember if fast.
Not sure if your a is always single element or just in this example.
% input: create arrays "a" and "b"
a = {'A23'};
l = 'A' : 'Z';
rl = reshape(l,size(l,2),1);
for i = 1 : size(rl,1)
    for j = 1 : 10000
        f{i,j} = sprintf('%s%d',rl(i), j);
    end
end
b = reshape(f,[],1);
% still part of the input: randomly replace 50 values of "b" with {'A23'}
r = round((size(b,1)-1) .* rand(50,1) + 1);
b(r) = a;
% How can I make this part way much faster ?
tic
[~,idx2] = ismember(string(b),string(a));
b_filtered = b(find(idx2),:);
toc
tic
tf = ismember(b,a);
b_filtered = b(tf,:);
toc
% only when a is scalar
tic
b_filtered = b(strcmp(b, a{1}));
toc
3 Commenti
  Bruno Luong
      
      
 il 18 Nov 2022
				AFAIK ismembc work on numbers, not char array or string, and the second argumentr must be sorted. So it is NOT applicable in your case.
Personally I know this function but I never use it since it is undocumented and I never need to draw the last ounce of speed for ismember.
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Multidimensional Arrays 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!