Thread Subject: how to use ndgrid with N dimensions

Subject: how to use ndgrid with N dimensions

From: Florian

Date: 25 Nov, 2009 17:26:21

Message: 1 of 2

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

Subject: how to use ndgrid with N dimensions

From: Matt

Date: 25 Nov, 2009 17:56:05

Message: 2 of 2

"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);

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com