MATLAB: How to manipulate dense 4D data

4-d data

hi,
I have a 4-D array. I plot it as using the code below(You need the function 'plotgrid' below as well). Based on the plot the spacing of the cube is very dense, I want to make the inter-spacing in the cube plot sparser than it currently is. But I dont know how to resample 4D data (or skip rows). Can anyone help ?
clc;clear all;close all;
%%%%%Test script
[x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2);
v = x.*exp(-x.^2-y.^2-z.^2);
xslice = [-1.2,.8,2]; yslice = 2; zslice = [-2,0];
slice(x,y,z,v,xslice,yslice,zslice)
colormap hsv
% I work with NDGRID rather than MESHGRID
[x,y,z] = ndgrid(-2:0.1:2,-2:.1:2,-2:.1:2);
xyz = cat(4, x, y, z);
plotgrid(gca, xyz, 'color', [0 0.5 0]);
%----------------------------------------%
function h = plotgrid(ax, xyz, varargin)
% function h = plotgrid(ax, xyz, varargin)
% Plot the 4D position array xyz(nx,ny,nz,3) in a grid-like plot
if isempty(ax)
ax = gca();
end
hold(ax, 'on');
h = [];
for dim=1:3
p = 1:4;
p([1 dim]) = [dim 1];
a = permute(xyz, p);
m = size(a,1);
a = reshape(a, m, [], 3);
if m > 1
hd = plot3(ax, a(:,:,1), a(:,:,2), a(:,:,3), '.-', varargin{:});
else
hd = plot3(ax, a(:,:,1), a(:,:,2), a(:,:,3), '.', varargin{:});
end
h = [h; hd];
end
hold(ax, 'off');
end % plotgrid

Best Answer

You can resample using interpn
You can also use every [p, q, r]'th point along the first three dimensions using reducevolume(). It is not documented clearly but the code for reducevolume is set up so that any higher dimensions are kept intact.