How do I use interp3 with grid vectors to interpolate a 3-D int8 array?

12 views (last 30 days)
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
Hari on 11 Jun 2025
Hi,
I understand that you are trying to resize a 3D int8 voxel array using 'nearest' interpolation and want to do so using either grid vectors or meshgrids. However, you’re encountering errors related to mismatched grid dimensions and invalid data types.
I assume the main issue is due to the use of int8 data with interp3, which only supports single or double types, and also the mismatch between grid vectors and the format expected by interp3.
In order to perform nearest-neighbor interpolation correctly on a 3D array, you can follow these steps:
Step 1: Convert your voxel array from int8 to double, as interp3 does not support integer types.
Step 2: When using meshgrids, ensure that the dimensions of the grids match the dimensions of the data array exactly. Also, be careful about the axis order — for 3D, meshgrid requires a specific order to align with interp3.
Step 3: Use linspace to define the new sampling points for each axis. This will generate uniformly spaced points matching your target resolution.
Step 4: Interpolate using 'nearest' as the method to preserve discrete values. Then, convert the output back to int8 if needed.
Step 5: Ensure that the reshaped array dimensions match your intended resized resolution to verify correct interpolation.
References:
Hope this helps!

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!