tensor product spline evaluation

3 views (last 30 days)
Jack
Jack on 30 Oct 2014
Answered: Aditya on 22 Jul 2025
Hello everyone:
Background is that I have a set of 3D points (x,y,z) and would like to know if y are under a B-spline . My idea is to substitute x and y into spline equations and use point-membership classification.
example I am using is from spline toolbox.
% create data
x = sort([(0:10)/10,.03 .07, .93 .97]);
y = sort([(0:6)/6,.03 .07, .93 .97]);
[xx,yy] = ndgrid(x,y); z = franke(xx,yy);
% fitting y
ky = 3; knotsy = augknt([0,.25,.5,.75,1],ky);
sp = spap2(knotsy,ky,y,z);
% fitting x
coefsy = fnbrk(sp,'coefs');
kx = 4; knotsx = augknt([0:.2:1],kx);
sp2 = spap2(knotsx,kx,x,coefsy.');
coefs = fnbrk(sp2,'coefs').';
% plot results
xv = 0:.025:1; yv = 0:.025:1;
values = spcol(knotsx,kx,xv)*coefs*spcol(knotsy,ky,yv).';
mesh(xv,yv,values.'), view(150,50);
% random points in 3D
pts=rand(100,3);
% evulation
for j=1:size(pts,1)
zEst(j,:) = fnval(spmak({knotsx,knotsy},coefs),{pts(j,1),pts(j,2)});
end
[IX,~]=find(pts(:,3)-zEst<0);
My question is: is it possible to use one function that is able to substitute all x and y values rar than put m into a for loop
I have try spcol but not working as point sequence TAU should be nondecreasing.
spcol(knotsx,kx,pts(j,1))*coefs*spcol(knotsy,ky,pts(j,2)).';
Thank You

Answers (1)

Aditya
Aditya on 22 Jul 2025
Hi jack,
You can efficiently evaluate a bivariate B-spline surface at many arbitrary (x, y) points without a for-loop by using MATLAB's fnval function in a vectorized manner. The key is to pass your x and y coordinates as row vectors inside a cell array to fnval, which will then return the estimated z-values for all points at once. This is much faster and cleaner than looping through each point. For example, if your random 3D points are stored in pts, you can compute all estimated surface z-values like this:
zEst = fnval(spmak({knotsx, knotsy}, coefs), {pts(:,1).', pts(:,2).'});
zEst = zEst(:); % Ensure it's a column vector
This approach avoids the need for a for-loop entirely and leverages MATLAB's optimized spline evaluation routines. There's no need to use spcol directly for this task, as fnval handles the details internally and works correctly for scattered input points.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!