Cellfun version of a code
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposta accettata
Walter Roberson
il 28 Ago 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!