how to manipulate dense 4D data ?

6 views (last 30 days)
Kate on 3 Jan 2014
Commented: Kate on 3 Jan 2014
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];
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();
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{:});
hd = plot3(ax, a(:,:,1), a(:,:,2), a(:,:,3), '.', varargin{:});
h = [h; hd];
hold(ax, 'off');
end % plotgrid

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jan 2014
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.

Sign in to comment.

More Answers (0)


Find more on Line Plots 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!