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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by