Speeding up comparison using strcmp
    12 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Yongmin
 am 13 Jan. 2015
  
    
    
    
    
    Kommentiert: Titus Edelhofer
    
 am 13 Jan. 2015
            Hello! I have a list of approximately 2 million records and I would like to compare the records with a list of devices which generates those records. My code is as follows where "c" is the list of records and "device" is the list for distinct devices:
for ii = 1:length(device)
    idx = ( strcmp(c,device(ii,:)) );
    lidx = find(idx);
    devid{ii} = lidx;
end
The problem is the above code takes too long time (more than an hour). Would you please tell me know how to reduce execution time?
Many thanks!
2 Kommentare
Akzeptierte Antwort
  Titus Edelhofer
    
 am 13 Jan. 2015
        Hi,
I would convert device to a cell array (using cellstr) and then call ismember without the loop, something like
cellDevice = cellstr(device);
[~, devid] = ismember(cellDevice, records);
Titus
3 Kommentare
  Titus Edelhofer
    
 am 13 Jan. 2015
				I understand. In this case it might be hard without a loop. I'm not sure, but something like this could work then:
[~,idx] = ismember(records, cellDevice);
devid = cell(numel(cellDevice), 1);
for ii=1:length(devid)
  devid{ii} = find(idx==ii);
end
Titus
Weitere Antworten (1)
  David Sanchez
      
 am 13 Jan. 2015
        If you have getnameidx available in your system, you might transform your device to a cell:
device_cell = celstr(device);
and then look for their position within c:
device_positions = getnameidx(c,device_cell);
which will return the position of your devices within the c cell
Siehe auch
Kategorien
				Mehr zu Logical finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!