how to manipulate dense 4D data ?

조회 수: 4 (최근 30일)
Kate
Kate 2014년 1월 3일
댓글: Kate 2014년 1월 3일
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

채택된 답변

Walter Roberson
Walter Roberson 2014년 1월 3일
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
Walter Roberson
Walter Roberson 2014년 1월 3일
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 2014년 1월 3일
thanks walter

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by