Cellfun version of a code

1 view (last 30 days)
Hari
Hari on 28 Aug 2015
Commented: Hari on 29 Aug 2015
Hi,
I'm using Matlab version 2015a on Windows 10.
I have a function "SomeFuncOfDeterminant" that takes 5 inputs and returns one output ( see end of message for detailed definition)
Presently when I call the function with specific values of input it returns the expected output correctly.
OutputValue = SomeFuncOfDeterminant(StartPointsForFinalOpt(1,1:2), ExpectedRank, DesignMatrixMultF{CurrExpRunCnter}, InputData(1,1:NumOfParms), InputData(1,end))
OutputValue =
3.153125081308734e+03
Info on arguments:
a) StartPointsForFinalOpt(1,1:2) - numeric vector consisting of +ve real numbers
b) ExpectedRank - natural number
c) DesignMatrixMultF{CurrExpRunCnter} - Function handle that takes 2 numeric vectors (all the inputs are +ve real numbers)
d) InputData(1,1:NumOfParms) - numeric vector consisting of +ve real numbers
e) InputData(1,end) - positive real number
I need to evaluate SomeFuncOfDeterminant for all the rows in "InputData" (so 4th and 5th argument will change) while initial 3 arguments will remain changed.
I used a parfor loop with minor changes and it works syntactically well but still very slow (this function is utlimately used for optimization and a single run takes upwards of 30 seconds or so..so speeding up evaluation of this function would help).
I would like to check if doing Cellfun instead of parfor will make the function evaluation faster (I guess it may not, but would like to try before rejecting..open to other approaches).
I tried following version of cellfun but it gives me an error saying not enough arguments
OutputValues=cell2mat(cellfun(SomeFuncOfDeterminant,num2cell(repmat(StartPointsForFinalOpt(1,1:2),size(InputData,1),1),2), num2cell(repmat(ExpectedRank,size(InputData,1),1),2), repmat({DesignMatrixMultF{CurrExpRunCnter}},size(InputData,1),1) ,...
num2cell(InputData(:,1:NumOfParms),2),num2cell(InputData(:,end),2),'UniformOutput',false));
The exact error message I get is:
Error using SomeFuncOfDeterminant (line 3)
Not enough input arguments.
I have been fiddling with various ways to overcome the error but not able to?
Also the 3rd argument for function handle; Is the way I have replicated it correct? Issue is right now the function handle is inside a cell so when it gets passed inside "SomeFuncOfDeterminant" I would have to change DesignMatrixMultF to DesignMatrixMultF{1} for it to correctly execute?
Thanks
Hari
% Definition of SomeFuncOfDeterminant
function [DetFunc] = SomeFuncOfDeterminant(X, ExpRank, DesignMatrixMultF,InputData1,InputData2)
if (ExpRank == rank(DesignMatrixMultF{1}(InputData1,X)))
TmpVal = 1/det(DesignMatrixMultF{1}(InputData1,X));
if TmpVal <=0
TmpVal = NaN;
end
else
TmpVal = NaN;
end
if isnan(TmpVal)
DetFunc = [NaN];
else
% Some function of the determinant - ULTIMATE focus
DetFunc = [(TmpVal/InputData2)^(1/ExpRank)];
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 28 Aug 2015
OutputValues = cell2mat( cellfun( @SomeFuncOfDeterminant, num2cell( repmat(StartPointsForFinalOpt(1,1:2),size(InputData,1),1), 2), num2cell( repmat(ExpectedRank,size(InputData,1),1), 2), repmat( {DesignMatrixMultF{CurrExpRunCnter}}, size(InputData,1),1), num2cell( InputData(:,1:NumOfParms), 2), num2cell( InputData(:,end), 2), 'UniformOutput', false));
The important change: adding @ before SomeFuncOfDeterminant
By the way, I can guarantee you that this cellfun approach will be slower than using a "for" loop. cellfun is implemented using loops. num2cell() is implemented using loops.
The for loop in turn might or might not be faster than parfor. parfor can have a lot of overhead in starting up so it is not always faster.
Sometimes "distributed arrays" are faster. And depending on what the code is doing, using gpuarrays can be faster.
You should also be considering bsxfun.
Calculations based on determinants are usually numerically somewhat unstable. You should be reconsidering your problem to try to remove the determinants such as by using the "\" operator.
  1 Comment
Hari
Hari on 29 Aug 2015
Thanks for the tip on @. Feeling little embarassed!
I will check out bsxfun. I have one follow question around speed of execution. I tried all the three below (parfor, for and cellfun) and cellfun was fastest even after running many times?
tic;
parfor ChkCnter = 1:size(InputData,1)
OutputValue1(ChkCnter,:) = SomeFuncOfDeterminant(StartPointsForFinalOpt(1,1:2), ExpectedRank, DesignMatrixMultF{CurrExpRunCnter}, InputData(ChkCnter,1:NumOfParms), InputData(ChkCnter,end));
end;
toc
tic;
for ChkCnter = 1:size(InputData,1)
OutputValue2(ChkCnter,:) = SomeFuncOfDeterminant(StartPointsForFinalOpt(1,1:2), ExpectedRank, DesignMatrixMultF{CurrExpRunCnter}, InputData(ChkCnter,1:NumOfParms), InputData(ChkCnter,end));
end;
toc
tic;
OutputValues3=cell2mat(cellfun(@SomeFuncOfDeterminant,num2cell(repmat(StartPointsForFinalOpt(1,1:2),size(InputData,1),1),2), num2cell(repmat(ExpectedRank,size(InputData,1),1),2), repmat({DesignMatrixMultF{CurrExpRunCnter}},size(InputData,1),1) ,...
num2cell(InputData(:,1:NumOfParms),2),num2cell(InputData(:,end),2),'UniformOutput',false));
toc;
Elapsed time is 0.280696 seconds.
Elapsed time is 0.171031 seconds.
Elapsed time is 0.144567 seconds.

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!