When testing function, output is same size and class as inputs, but during parameter fitting I receive error: Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data.

2 views (last 30 days)
Hello,
I am trying to use a custom Temporal Difference Learning function that I have used prior. I modified it slightly to only output the "V" variable column-wise and to take a single column "stim" with the numbers 1, 2, 3, 4, or 5. Based on the numbers, it will create a matrix of 0s and that it the length of the number of trials (length of stim) and will add 1 for each trial depending on the column denoted by the number in stim (1, 2, 3, 4, 5) for that trial (I know this is not the cleanest way to do it but it was only way for it to work in fittype function).
The function appears as below:
function model = TD_Fit(x,r,alpha,g)
model = [];
% create 5 columns 0s with 1 denoting if number is present in column
% for that trial
stim = x;
X = [];
for j=1:length(stim)
if stim(j) == 1
X(j, :) = [1 0 0 0 0];
elseif stim(j) == 2
X(j, :) = [0 1 0 0 0];
elseif stim(j) == 3
X(j, :) = [0 0 1 0 0];
elseif stim(j) == 4
X(j, :) = [0 0 0 1 0];
elseif stim(j) == 5
X(j, :) = [0 0 0 0 1];
end
end
% initialization
[N,D] = size(X);
w = zeros(D,1); % weights
X = [X; zeros(1,D)]; % add buffer at end
% run Kalman filter
for n = 1:N
h = X(n,:) - g*X(n+1,:); % temporal difference features
V = X(n,:)*w; % value estimate
rhat = h*w; % reward prediction
dt = r(n) - rhat; % prediction error
w = w + alpha*dt*h'; % weight update
model = [model; V];
end
end
The function works independently of curve fitting. I have tested it out and it produces the expected outcome. The two inputs and 139x1 double variables and the output is a 139x1 double variable. They are the same class and the same size.
However, when I try curve fitting in order to determine the best fitting coefficients of alpha and g, both in the GUI app and as below:
[xData, yData, zData] = prepareSurfaceData( stim, r, outcome );
ft = fittype( 'TDFit2(x,y,alpha,g)', 'independent', {'x', 'y'}, 'dependent', 'z' );
I receive the below error each time I try to curve fit the function on my data.
Error using fittype/testCustomModelEvaluation (line 16)
Custom equations must produce an output vector, matrix, or array that is the same size and shape as the input data. This custom equation fails to
meet that requirement:
TD_Fit(x,y,alpha,g)
Error in fittype>iCreateFittype (line 373)
testCustomModelEvaluation( obj );
Error in fittype (line 330)
obj = iCreateFittype( obj, varargin{:} );
I am not sure why this is happening as I have checked the size and class and they are the same outside of curve fitting or fit type. Below is size and class of each variable and "a" is the output variable for one example test outside of fittype.
Screen Shot 2019-05-28 at 12.48.56 PM.png
Anyone know why this error would happen if the output is the same class and size of input variables outside of curve fitting? I am running out of ideas and have no idea how to fix if the variables are all doubles and the same size when running outside of curve fitting.

Answers (1)

YX Sun
YX Sun on 15 Oct 2020
I got the same error message when I was trying to combine two sets of data into a single column vector. I tried both x=[x,x], y=[y1,y2] and x=[x;x], y=[y1;y2] where x=4640x1, y1=4640x1, and y2=4640x1. fit(x,y, ft) produced the same error message as you reported here.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!