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

 Réponse acceptée

Walter Roberson
Walter Roberson le 3 Jan 2014

0 votes

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.

7 commentaires

hi walter,
I tried using interpn, but get an error when I try to plot. Please see code below if you get a chance (PS: it uses the 'plotgrid' function above in original post). thanks.
%%%%%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]);
[xq,yq,zq,tq] = ndgrid(-2:0.1:2,-2:0.1:2,-2:0.1:2,-2:0.1:2);
Vq = interpn(xyz,xq,yq,zq,tq);
figure,
plotgrid(gca, Vq, 'color', [0 0.5 0]);
Walter Roberson
Walter Roberson le 3 Jan 2014
What error do you get? What does size(Vq) indicate ?
hi Walter,
the error is :
Error using reshape
Product of known dimensions, 123, not divisible
into total number of elements, 2825761.
The size of my original 4-D array named 'xyz' is [41 41 41 3]. After applying interpn, the new array named 'Vq' is [41 41 41 41].
I am not sure if I am applying the interpn correctly to resample. thanks
The gridplot documentation does say,
% Plot the 4D position array xyz(nx,ny,nz,3) in a grid-like plot
I suspect now that what you want is your original code, except with
[x,y,z] = ndgrid(-2:0.1:2,-2:.1:2,-2:.1:2);
replaced with something like
[x,y,z] = ndgrid(-2:0.2:2,-2:0.2:2,-2:0.2:2);
or for finer control,
numlines = 11; %for example
[x,y,z] = ndgrid(linspace(-2,2,numlines), linspace(-2,2,numlines), linspace(-2,2,numlines));
In this special case of wanting the same grid in each of the dimensions you can abbreviate this to
[x,y,z] = ndgrid(linspace(-2,2,numlines));
Kate
Kate le 3 Jan 2014
Walter,
thanks, but, the issue is that I am given the dense 4-D matrix (I do not generate it) and want to reduce to a sparser form.
In my original test code above, I generated a dense 4D array only for testing purposes.
thanks again
If you have an xyzv matrix and you want every P'th point in the x, y, z dimensions, leaving v (because it is only 3 wide), then
newmatrix = xyzv(1:P:end, 1:P:end, 1:P:end, :);
This simply drops points. If your matrix varies quickly enough in value then you might prefer to interpolate instead of simply dropping points. If you want to be left with N points along each of x, y, z, keeping 3 along v, then:
SZ = size(xyzv);
[xq,yq,zq,tq] = ndgrid(linspace(1,SZ(1),N), linspace(1,SZ(2),N), linspace(1,SZ(3),N), 1:SZ(4));
newmatrix = interpn(xyzv, xq, yq, zq, tq);
Kate
Kate le 3 Jan 2014
thanks walter

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by