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

David Sanchez
David Sanchez 2015년 1월 13일
What do you mean by "list".
Are c and device sell arrays?
Yongmin
Yongmin 2015년 1월 13일
편집: Yongmin 2015년 1월 13일
Yes, c is cell array and device is array of character strings.

댓글을 달려면 로그인하십시오.

 채택된 답변

Titus Edelhofer
Titus Edelhofer 2015년 1월 13일

1 개 추천

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

Yongmin
Yongmin 2015년 1월 13일
Many thanks for your answer. The number of distinct device is approximately 110k and the records for each device can be multiple not just one. So I need to find all the records that contain each device. Would you please help me to refine the code?
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
Yongmin
Yongmin 2015년 1월 13일
Wow, the method you told is much faster. Thank you so much for your help!!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Sanchez
David Sanchez 2015년 1월 13일

1 개 추천

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

댓글 수: 3

Titus Edelhofer
Titus Edelhofer 2015년 1월 13일
Hi David,
this function is from Financial Toolbox. And although it generally speaking is doing what Yongmin wants, it does not handle the multiple occurrences (from the help: NOTE: It will not find multiple occurrences of a name ...). Titus
Yongmin
Yongmin 2015년 1월 13일
편집: Yongmin 2015년 1월 13일
Many thanks for your answer. But I don't have getnameidx in my system. Would you please tell me how to get it?
Titus Edelhofer
Titus Edelhofer 2015년 1월 13일
As I said, it's in the Financial Toolbox, but it won't help for your problem.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2015년 1월 13일

댓글:

2015년 1월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by