This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Create array on GPU


G = gpuArray(X)


G = gpuArray(X) copies the numeric array X to the GPU, and returns a gpuArray object. You can operate on this array by passing its gpuArray to the feval method of a CUDA kernel object, or by using one of the methods defined for gpuArray objects in Establish Arrays on a GPU.

The MATLAB array X must be numeric (for example: single, double, int8, etc.) or logical, and the GPU device must have sufficient free memory to store the data.

If the input argument is already a gpuArray, the output is the same as the input.

Use gather to retrieve the array from the GPU to the MATLAB workspace.


Transfer a 10-by-10 matrix of random single-precision values to the GPU, then use the GPU to square each element.

X = rand(10,'single');
G = gpuArray(X);
G2 = G .* G;         % Performed on GPU
whos G2              % Result on GPU
  Name       Size      Bytes  Class      Attributes

  G2        10x10          4  gpuArray

Copy the array back to the MATLAB workspace.

G1 = gather(G2);
whos G1
  Name       Size      Bytes  Class      Attributes

  G1        10x10        400  single

Introduced in R2010b

Was this topic helpful?