Problem with Fminsearch function

% Load the data
idxMiddle = blk.audDiff == 0;
audMiddle = [blk.visDiff(idxMiddle,1) blk.responseMade(idxMiddle)];
audMiddle = audMiddle(~audMiddle(:,2)==0,:);
ccSym = blk.visValues;
tn = arrayfun(@(x) length(audMiddle(audMiddle(:,1)==x,2)==2), ccSym);
meanProb = arrayfun(@(x) mean(audMiddle(audMiddle(:,1)==x,2)==2) , ccSym);
reactionLength = blk.responseTime;
% % Do the fitting
parstart = [3 0.5 0.5 0 0.1]; %Initial parameters
nfits = 10; %Number of iterative fits
minErrors = zeros(nfits,1); %Vector to keep track of errors with each fit
pars = cell(nfits,1); %Cell array to keep track of parameters for each fit
%Loop to fit data 10 times
for i=1:nfits
erHandle = @(pars)(errorFun(pars ,[ccSym'; tn'; meanProb'; reactionLength]));
options = optimset('MaxFunEvals', 1000);
pars{i} = fminsearch(erHandle, parstart, options);
minErrors(i) = errorFun(pars{i}, [ccSym'; tn'; meanProb'; reactionLength]);
end
[L,iBestFit] = min(minErrors);
bestPars = pars{iBestFit};
I have a problem with this program, and I'm receiving these errors when I try to run it:
>> model
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in model>@(pars)(errorFun(pars,[ccSym';tn';meanProb';reactionLength]))
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in model (line 52)
pars{i} = fminsearch(erHandle, parstart, options);
What would you suggest me to do? Thank you in advance.

5 Comments

Use the size function to determine the dimensions of: ccSym, tn, meanProb, and reactionLength.
You can also use the whos function, for example:
x = rand(3,2);
whos('x')
Name Size Bytes Class Attributes
x 3x2 48 double
that gives a bit more information.
Posting the results to a Comment here would help us to determine the problem.
Hi,
Sorry for the late reply, I had to change some parts of the code as reactionLength was not the same size as the other ones.
This is what I did:
audMiddle_time = [blk.timeToWheelMove(idxMiddle) blk.responseMade(idxMiddle) blk.visDiff(idxMiddle,1)];audMiddle_time = audMiddle_time(audMiddle_time(:,2)==2,:); reactionLength = arrayfun(@(x) median(audMiddle_time(audMiddle_time(:,1)==x,2)==2), ccSym);
But I don't understand why do I get the reactionLength in a logical form. How can I change that? I still have the same errors, and I think it's because of this.
>> whos('tn')
Name Size Bytes Class Attributes
tn 9x1 72 double
>> whos('reactionLength')
Name Size Bytes Class Attributes
reactionLength 9x1 9 logical
>> whos('meanProb')
Name Size Bytes Class Attributes
meanProb 9x1 72 double
>> whos('ccSym')
Name Size Bytes Class Attributes
ccSym 9x1 72 double
You forgot to transpose "reactionLength" in
[ccSym'; tn'; meanProb'; reactionLength]
Oh, yeah, but I still have the same errors :(
What about
erHandle = @(pars)(errorFun(pars ,[ccSym.'; tn.'; meanProb.'; (double(reactionLength)).']));

Sign in to comment.

Answers (0)

Asked:

on 7 Jun 2019

Commented:

on 12 Jun 2019

Community Treasure Hunt

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

Start Hunting!