Build matrices or N-D arrays by replicating some template vectors (2024)

Syntax

[X, Y] = ndgrid(x)[X, Y] = ndgrid(x,y)[X, Y, Z] = ndgrid(x,y,z)[X, Y, Z, T] = ndgrid(x,y,z,t)[X1, X2, ..., Xm] = ndgrid(x1,x2,...,xm)

Arguments

x, y, z, ...

vectors of any data types. They may have distinct data types. By default, y=x.

X, Y, Z, ...

matrices in case of 2 input arguments, or hypermatrices otherwise. They all have the same sizes: size(x,"*") rows, size(y,"*") columns, size(z,"*") layers, etc. They have the datatypes of respective input vectors: typeof(X)==typeof(x), typeof(Y)==typeof(y), etc.

Description

The first application of ndgrid is to build a grid of nodes meshing the 2D or 3D or N-D space according to 2, 3, or more sets x, y, etc.. of "template" coordinates sampled along each direction/dimension of the space that you want to mesh.

Hence, the matrix or hypermatrix X is made by replicating the vector x as all its columns ; the matrix or hypermatrix Y is made by replicating the vector y as all its rows ; Z is made of replicating the vector z along all its local thicknesses (3rd dimension); etc.

--> [X, Y] = ndgrid([1 3 4], [0 2 4 6]) X = 1. 1. 1. 1. 3. 3. 3. 3. 4. 4. 4. 4. Y = 0. 2. 4. 6. 0. 2. 4. 6. 0. 2. 4. 6.

Then, the coordinates of the node(i,j) in the 2D space will be simply [x(i), y(j)] now given by [X(i,j), Y(i,j)]. As well, the coordinates of a node(i,j,k) of a 3D grid will be [x(i), y(j), z(k)] now given by [X(i,j,k), Y(i,j,k), Z(i,j,k)].

This replication scheme can be generalized to any number of dimensions, as well to any type of uniform data. Let's for instance consider 2 attributes:

  1. The first is a number, to be chosen from the vector say n= [ 3 7 ]
  2. The second is a letter, to be chosen from the vector say c= ["a" "e" "i" "o" "u" "y"]

Then we want to build the set of all {n,c} possible pairs. It will just be the 2D grid:

--> [N, C] = ndgrid([3 7],["a" "e" "i" "o" "u" "y"]) C =!a e i o u y !!a e i o u y ! N = 3. 3. 3. 3. 3. 3. 7. 7. 7. 7. 7. 7.

Then, the object(i,j) will have the properties {n(i) c(j)} that now can be addressed with {N(i,j) C(i,j)}. This kind of grid may be useful to initialize an array of structures.

Following examples show how to use X, Y, Z in most frequent applications.

Examples

Example #1:

// Create a simple 2d gridx = linspace(-10,2,40);y = linspace(-5,5,40);[X,Y] = ndgrid(x,y);// Compute ordinates Z(X,Y) on the {X, Y} grid and plot Z(X,Y)Z = X - 3*X.*sin(X).*cos(Y-4) ;clf()plot3d(x,y,Z, flag=[color("green") 2 4], alpha=7, theta=60); show_window()

Build matrices or N-D arrays by replicating some template vectors (1)

Example #2:

// Create a simple 3d gridnx = 10; ny = 6; nz = 4;x = linspace(0,2,nx);y = linspace(0,1,ny);z = linspace(0,0.5,nz);[X,Y,Z] = ndgrid(x,y,z);// Try to display this 3d gridXF=[]; YF=[]; ZF=[];for k=1:nz [xf,yf,zf] = nf3d(X(:,:,k),Y(:,:,k),Z(:,:,k)); XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];endfor j=1:ny [xf,yf,zf] = nf3d(matrix(X(:,j,:),[nx,nz]),... matrix(Y(:,j,:),[nx,nz]),... matrix(Z(:,j,:),[nx,nz])); XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];endclf()plot3d(XF,YF,ZF, flag=[0 6 3], 66, 61,leg="X@Y@Z")xtitle("A 3d grid !"); show_window()

Build matrices or N-D arrays by replicating some template vectors (2)

Example #3: Creates a table of digrams:

[c1, c2] = ndgrid(["a" "b" "c"], ["a" "b" "c" "d" "e" "f" "g" "h"])c1+c2
--> [c1, c2] = ndgrid(["a" "b" "c"], ["a" "b" "c" "d" "e" "f" "g" "h"]) c2 =!a b c d e f g h !!a b c d e f g h !!a b c d e f g h ! c1 =!a a a a a a a a !!b b b b b b b b !!c c c c c c c c !--> c1+c2 ans =!aa ab ac ad ae af ag ah !!ba bb bc bd be bf bg bh !!ca cb cc cd ce cf cg ch !

See also

  • meshgrid — Create matrices or 3-D arrays from vectors replicated in 2 or 3 dimensions
  • kron — Kronecker tensorial product. Weighted array replication
  • feval — multiple evaluation
  • nf3d — Rectangular facets to plot3d parameters

History

VersionDescription
6.0.0 Extension to all hom*ogeneous datatypes ([], booleans, encoded integers, polynomials, rationals, strings). Revision of the help page.
6.0.1 ndgrid(x) is now accepted: Default y=x.
Build matrices or N-D arrays by replicating some template vectors (2024)
Top Articles
Adena Mychart Sign In
Best Views in Amsterdam
Amc Near My Location
Pnct Terminal Camera
Regal Amc Near Me
PontiacMadeDDG family: mother, father and siblings
Stl Craiglist
Okatee River Farms
Jscc Jweb
อพาร์ทเมนต์ 2 ห้องนอนในเกาะโคเปนเฮเกน
Sarpian Cat
Oc Craiglsit
Seattle Rpz
Jackson Stevens Global
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
Powerball winning numbers for Saturday, Sept. 14. Check tickets for $152 million drawing
111 Cubic Inch To Cc
Pretend Newlyweds Nikubou Maranoshin
TBM 910 | Turboprop Aircraft - DAHER TBM 960, TBM 910
Recap: Noah Syndergaard earns his first L.A. win as Dodgers sweep Cardinals
Selfservice Bright Lending
EASYfelt Plafondeiland
Providence Medical Group-West Hills Primary Care
Gran Turismo Showtimes Near Marcus Renaissance Cinema
27 Paul Rudd Memes to Get You Through the Week
Silky Jet Water Flosser
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Watson 853 White Oval
Smartfind Express Login Broward
'Insidious: The Red Door': Release Date, Cast, Trailer, and What to Expect
Receptionist Position Near Me
Pronóstico del tiempo de 10 días para San Josecito, Provincia de San José, Costa Rica - The Weather Channel | weather.com
Phoenixdabarbie
O'reilly's In Monroe Georgia
A Plus Nails Stewartville Mn
Stolen Touches Neva Altaj Read Online Free
RUB MASSAGE AUSTIN
Www Violationinfo Com Login New Orleans
Leatherwall Ll Classifieds
Studentvue Columbia Heights
Can You Buy Pedialyte On Food Stamps
Frcp 47
Td Ameritrade Learning Center
Panorama Charter Portal
Tricia Vacanti Obituary
Craigslist Farm And Garden Reading Pa
Hanco*ck County Ms Busted Newspaper
What is 'Breaking Bad' star Aaron Paul's Net Worth?
Gander Mountain Mastercard Login
Sj Craigs
Marion City Wide Garage Sale 2023
Salem witch trials - Hysteria, Accusations, Executions
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5960

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.