MATLAB Answers

matrix dimension error for interpolation in loop

1 view (last 30 days)
Melissa on 14 Dec 2012
Good Afternoon All,
I seem to be having a bit of trouble with matrix dimensions agreeing (I know rookie mistake). I am using radial basis functions and have created a model which I want to evaluate. I seem to be having the error here:
for j = 1:nSites
r(j) = norm(x - rbfmodel.X(j,:)');
Error in evalRBF (line 21)
r(j) = norm(x - rbfmodel.X(j,:)');
where Nsites is the number of sites in the original data points (rbfmodel.X) and x is the vector of points to be evaluated.
I think that I need some sort of indexing for x, maybe something along the lines of:
r = zeros(nSites,nSites);
for i = 1:nSites
for j = 1:i-1
r(i,j) = norm(x(i,:) - rbfmodel.X(j,:));
r(1:i-1,i) = r(i,1:i-1);
Anyone have suggestions?
Here is the code for evalRBF
function fx = evalRBF(x,rbfmodel)
% EVALRBF(X,RBFMODEL)Evaluates a radial basis function surrogate at a given point
% Input:
% x = the point to be evaluated
% rbfmodel = structure of all parameters that define the RBF surrogate
% .X = matrix of data sites
% .kernel = string indicating the choice of kernel function
% .coeff = coefficients that define the RBF
% Output:
% fx = RBF value at x
% nSites is the number of data sites provided in Original Model
[nSites] = size(rbfmodel.X,1);
% r is the vector of distances between data sites
r = zeros(nSites,1);
for j = 1:nSites
r(j) = norm(x - rbfmodel.X(j,:)');
% Adjust x as necessary
if strcmp(rbfmodel.poly,'regpoly0')
x = [];
x = feval(typePoly,x');
x = x';
x(1,:) = [];
% Evaluate RBF at x
y = [kernelRBF(rbfmodel.kernel,r,rbfmodel.c); 1; x(:)];
fx = rbfmodel.coeff'*y;
if ~isfinite(fx)
fx = 1/eps;
Matt J
Matt J on 14 Dec 2012
Well first of all, it should be clear to you now where your bug is coming from. If x is always a 1x2 row vector (as with [50,50]) you will clearly not be able to subtract it from a 1x79 row vector (as rbfmodel.X(j,:) will be).
If you really want x to be a 2x1 column vector and you want it to be subtracted from every column of rbfmodel.X, change the relevant line as in my Answer below.

Sign in to comment.

Answers (1)

Matt J
Matt J on 14 Dec 2012
Edited: Matt J on 14 Dec 2012
A first guess
r(j) = norm(x(:) - rbfmodel.X(:,j));

Community Treasure Hunt

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

Start Hunting!