how to use ndgrid with N dimensions

  • Follow


Hi everyone, let me explain my problem with an example

% A two dimensional example:
% Let's assume you have the following two dimensional grid data points
X = [1 6;3 7;5 2;8 1;10 0];

% In order to use griddatan for interpolation I need to convert these into a monotonously spaced grid like:
[x1,x2] = ndgrid( min((X(:,1))):1:max(X(:,1)), min((X(:,2))):1:max(X(:,2)) );

% The following xi is then used in griddatan
xi = [x1(:) y2(:)];

Generally speaking I need a code which automatically splits up X in N subvectors for N dimensions, passes them to ndgrid and also generates x2-xn automatically to produce xi.

How can I automate this if I only know that my data has N dimensions (X has N columns)?

thanks for your help,
Florian
0
Reply Florian 11/25/2009 5:26:21 PM

"Florian" <flowwiththeflo@hotmail.com> wrote in message <hejpbt$s1b$1@fred.mathworks.com>...
> Hi everyone, let me explain my problem with an example
> 
> % A two dimensional example:
> % Let's assume you have the following two dimensional grid data points
> X = [1 6;3 7;5 2;8 1;10 0];
> 
> % In order to use griddatan for interpolation I need to convert these into a monotonously spaced grid like:
> [x1,x2] = ndgrid( min((X(:,1))):1:max(X(:,1)), min((X(:,2))):1:max(X(:,2)) );
> 
> % The following xi is then used in griddatan
> xi = [x1(:) y2(:)];
> 
> Generally speaking I need a code which automatically splits up X in N subvectors for N dimensions, passes them to ndgrid and also generates x2-xn automatically to produce xi.
> 
> How can I automate this if I only know that my data has N dimensions (X has N columns)?

Something like the following:

N=size(X,2);

Xmins=min(X,[],1);
Xmaxs=max(X,[],1);

for ii=1:N
 ndgridArgs{ii}=Xmins(ii):Xmaxs(ii);
end

[xi{1:N}]=ndgrid(  ndgridArgs{:} );

xi=cat(N+1, xi{:});

xi=reshape(xi,[],N);
0
Reply Matt 11/25/2009 5:56:05 PM


1 Replies
716 Views

(page loaded in 0.001 seconds)

Similiar Articles:













7/22/2012 2:54:48 AM


Reply: