2016-04-25 8 views
0

私は2つの大きなデータセットを持っています - 検索は340,000 x 1で、フィールドは348,000 x 2です。私の目標は検索で要素を使用し、フィールド(:、1)でその位置を見つけ、フィールド(:、2)を呼び出してresultという新しいセル配列を作成します。このルックアッププログラムの効率をどのように改善できますか?

cellfunを直接使用してメモリが不足していたため、データセットをサブセットに分割してから結果をコンパイルする必要がありました。

次のプログラムを作成しましたが、2時間40分という非常に長い時間がかかります。

私の質問はこれですが、このタスクをより効率的に実行するにはどうすればよいですか?既存のコードを変更する必要があるのですか、または問題を解決する際に全く異なるアプローチをとる必要がありますか?

function result = bigdatacmp(search,field) 

%BIGDATACMP(SEARCH,FIELD) takes strcmp jobs that require excessive amounts 
% memory and splits them up into manageable subsets. The results of the 
% subsets are then compiled to represent the original set. 


tic 

subsets = floor(size(search,1)/1000);  %Divides search into subsets 
difference = size(search,1) - 1000*subsets; %# of elements in last subset 

result = cell(0);       %Establish empty variables 

%Loops through all subsets. Finds location of matches in the first column 
%of field. Compiles subset locations. Compiles results from second column 
%of field. 
for i = 1:subsets 

    searchvalues = search(1000*i-999:1000*i); 

    Zlogic = cellfun(@(x)(strcmp(x,field(:,1))),... 
     search(1000*i-999:1000*i),'UniformOutput',false); 

    result(1000*i-999:1000*i) = cellfun(@(x)(field(x,2)),... 
     Zlogic,'UniformOutput',false); 
end 

%Performs same calculations as in loop, but for the final subset. 
Zlogic = cellfun(@(x)(strcmp(x,field(:,1))),search(size(search,1)-... 
    difference+1:size(search,1)),'UniformOutput',false); 

result(end+1:end+difference) = cellfun(@(x)(field(x,2)),Zlogic,... 
    'UniformOutput',false); 

result = result'; 

toc 
end 

答えて

1

348kはそれほど大きなものではありません。 fieldの最初の列から2番目の列の対応するエントリにオブジェクトをマッピングするcontainers.Mapオブジェクトを構築することを検討してください。次に、すべてのエントリに対してfieldの完全検索に相当するものをsearchに行う必要はありません。

[追加された編集:] 348kがエントリの総数であれば、それ以上分割する必要はないと思います。

+0

「containers.Map」を私と共有していただき、ありがとうございます。それは私の現在の問題を完全に解決し、将来も非常に有用であるように聞こえる。私は現在解決策を模索していますが、エラーが発生しました: 'containers.Map/valuesを使用しているエラー 指定されたキーがこのコンテナに存在しません.'これを回避する方法はありますか? –

+0

おそらく 'isKey'メソッドが必要です。 –

+0

ありがとう、ガレス。それはまさに私が探していたものです。 –

関連する問題