Ahmed Hamed
Ahmed Hamed

Reputation: 21

How to pass parameter to map function during calling mapreduce function?

I'have a mapreduce function whose output should be fed to another mapreduce function the code is as follows

function clustering = parallel_clustering_kmeans(data)
%% find first clustering from all chunks
result = mapreduce(data,@k_means_Mapper,@k_means_Reducer);
result = readall(result);
index = result{:,1};
index = cell2mat(cellfun(@str2num,strrep(index,',',' '),'un',0));
clustering = mapreduce(data,@k_means_Mapper_second,@k_means_Reducer);
end

The first function works well result = mapreduce(data,@k_means_Mapper,@k_means_Reducer); however, I want to pass the index as a parameter for the k_means_Mapper_second

The code must be as follows

function k_means_Mapper_second(data,index,intermidiateValuesOut)
distance = zeros(size(data,1),size(index,1));
parfor i = 1:size(data,1)
    for j = 1:size(index,1)
        distance(i,j) =  sum((data(i,:)-index(j,:)).^2).^0.5;
    end
end
for i = 1:size(distance,1)
    x = distance(i,:);
    [~,ind] = min(x);
    key = combine_values(data(i,:));
    addmulti(intermidiateValuesOut,key,ind);
end
end

My question is how can i pass the index as a parameter to the k_means_Mapper_second function in the last line

clustering = mapreduce(data,@k_means_Mapper_second,@k_means_Reducer);

Thanks in advance

Upvotes: 0

Views: 203

Answers (1)

Suever
Suever

Reputation: 65430

You need to wrap your function within an anonymous function that accepts all inputs given to it by mapreduce but then calls your function, passing only the relevant values. For your case it would look something like:

mapperFunc = @(data, info, interim)k_means_Mapper_second(data, index, interim);
clustering = mapreduce(data, mapperFunc, @k_means_Reducer);

Upvotes: 2

Related Questions