How do I use interp3 with grid vectors to interpolate a 3-D int8 array?
10 views (last 30 days)
Show older comments
I need to resize a 3D int8 array using 'nearest' interpolation. I cannot figure out how to do this for the life of me. I want to use grid vectors, but I can't get them to work. Here is my code...
voxels(resolution(1),resolution(2),resolution(3))=0; % Not in code. Included for size clarity.
voxels=interp3(...
0:(resolution(1)-1),...
0:(resolution(2)-1),...
0:(resolution(3)-1),...
voxels,...
0:((resolution(1)-1)/(100-1)):(resolution(1)-1),...
0:((resolution(2)-1)/(104-1)):(resolution(2)-1),...
0:((resolution(3)-1)/(66 -1)):(resolution(3)-1),...
'nearest'...
);
Alternatively I've tried using meshgrid, but the results are the same.
voxels(resolution(1),resolution(2),resolution(3))=0; % Not in code. Included for size clarity.
[x,y,z]=meshgrid(...
0:(resolution(1)-1),...
0:(resolution(2)-1),...
0:(resolution(3)-1) ...
);
[xq,yq,zq]=meshgrid(...
0:((resolution(1)-1)/(100-1)):(resolution(1)-1),...
0:((resolution(2)-1)/(104-1)):(resolution(2)-1),...
0:((resolution(3)-1)/(66 -1)):(resolution(3)-1) ...
);
voxels=interp3(x,y,z,voxels,xq,yq,zq,'nearest');
These both throw the error: The grid vectors do not define a grid of points that match the given values.
When 'nearest' isn't included I get: Sample values must be a single or double array.
Please explain to me what's wrong and how to properly use this function.
Answers (1)
Hari
on 11 Jun 2025
Hi,
I understand that you're trying to resize a 3D int8 voxel array using 'nearest' interpolation with interp3, and you're encountering errors related to grid compatibility and data types.
I assume that the core issues are:
- interp3 requires the input array to be of type single or double, not int8.
- You are mixing up grid vector and full grid (meshgrid) syntax in a way that interp3 does not accept.
In order to resize a 3D array using 'nearest' interpolation with interp3, you can follow the below steps:
Step 1: Convert your voxel array from int8 to double, as required by interp3. This avoids the data type error.
Step 2: If you're using grid vectors (not full meshgrids), you must ensure they are 1D vectors corresponding to each axis, and the dimensions of the data must match [length(y), length(x), length(z)].
Step 3: When defining the query points for interpolation, you must use full 3D grids (i.e., using meshgrid) if you are evaluating on a structured grid. Make sure the axis order matches how interp3 interprets the data.
Step 4: After interpolation, convert the result back to int8 to maintain the original data type.
References:
- https://www.mathworks.com/help/matlab/ref/interp3.html
- https://www.mathworks.com/help/matlab/ref/meshgrid.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Interpolation 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!