Cellfun version of a code
显示 更早的评论
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!