selectdata - Select slices of arrays and generate CUDA code - MATLAB (original) (raw)
Select slices of arrays and generate CUDA code
Since R2025a
Syntax
Description
[B](#mw%5F3b843c42-8baf-4336-bd57-87c13f9d11dd) = selectdata([A](#mw%5F96ddc2b8-5d92-47b5-8d63-4c4aa18e2b17),[pred](#mw%5F8d81f07a-7413-40f5-9c64-496a0fc521a4))
returns an array with the slices of A
that cause the predicate functionpred
to return true
.
[B](#mw%5F3b843c42-8baf-4336-bd57-87c13f9d11dd) = selectdata(___,[Name=Value](#namevaluepairarguments))
specifies options using one or more name-value arguments in addition to the input arguments in the previous syntax. For example, specify the dim dimension to select in the input array.
[[B](#mw%5F3b843c42-8baf-4336-bd57-87c13f9d11dd),[I](#mw%5F0902be26-35c4-496c-a9dc-62aeab5a3db3)] = selectdata(___)
also returns the indices, I
, of the selected slices.
Examples
Create a function removeZeros
that usesselectData
to remove zeros from a vector. Because the input argument is a vector, selectdata
slices the vector by passing individual elements into the predicate function handle.
function v=removeZeros(u) %#codegen pred = @(slice)slice~=0; v=selectdata(u, pred); end
Call removeZeros
to remove zeros from vectoru
.
u = [1 0 2 0 3 0 4 0 5 0 6 0]; v = removeZeros(u)
Create a GPU code configuration object and generate code fromremoveZeros
.
cfg = coder.gpuConfig("mex"); codegen removeZeros -args {u} -config cfg;
Use the slice
and idx
arguments to select odd-numbered columns where all entries are less than 0.75.
Create a 2-by-7 array, A
, whose entries are in the interval (0, 1).
A = [0.9228 0.0968 0.4245 0.8835 0.0595 0.8022 0.5921; 0.1897 0.9151 0.5221 0.3348 0.1093 0.9166 0.2709];
Create a function named selectOddIndicesInRange
. In that function, create a predicate function handle, pred
, that takes a two element slice and an index. The predicate function returns true
for columns where both elements are less than 0.75 and the index is odd. Callselectdata
with dim
set to2
to select from the columns of the array.
function B = selectOddIndicesInRange(A) %#codegen pred = @(slice,idx)slice(1) < 0.75 & slice(2) < 0.75 & mod(idx,2)==1; B = selectdata(A,pred,dim=2); end
Call the selectOddIndicesInRange
function.
B = selectOddIndicesInRange(A)
B =
0.4245 0.0595 0.5921
0.5221 0.1093 0.2709
Create a GPU code configuration object and generate code fromselectOddIndicesInRange
.
cfg = coder.gpuConfig("mex"); codegen selectOddIndicesInRange -args {A} -config cfg
Select three-element rows where each entry is in the RGB coordinate range of [0, 255].
Create a 4-by-3 array named A
.
A = [42 197 228; 126 10 223; 275 255 118; 288 204 51];
To select three-element slices of A
, select data from the rows of the array. Create a function named selectRGB
with a predicate function that verifies all elements of the array are between 0 and 255.
function B = selectRGB(A) %#codegen pred = @(slice) all(0 <= slice & slice <= 255); B = selectdata(A,pred,postprocess=@(x)uint32(x)); end
The function postprocesses the array by casting the resulting array to theuint32
data type. Select the RGB coordinates fromA
.
B =
2×3 uint32 matrix
42 197 228
126 10 223
Create a GPU code configuration object and generate code fromselectRGB
.
cfg = coder.gpuConfig("mex"); codegen selectRGB -args {A} -config cfg;
Input Arguments
Input array, specified as a vector or array.
Predicate function to use to select slices of the array, specified as a function handle. The predicate function must accept one or two arguments and return a logical scalar. The predicate function must have one of these function signatures:
tf = pred(slice)
takes an input arrayslice
. The function takes eachslice
by slicingA
into arrays along the dimensiondim. For example, ifdim
is1
, andA
is a 3-by-5 array, the predicate function takes the 1-by-5 slicesA(1,:)
,A(2,:)
, andA(3,:)
.tf = pred(slice,idx)
also accepts the index,idx
, that corresponds to the slice. For example, ifA
is a three-dimensional array anddim
is2
, then the inputslice
is equal toA(:,idx,:)
.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Example: B = selectdata(A,pred,dim=2)
Dimension to select in the input array, specified as a positive integer. For example, if A is a two-dimensional array anddim
is 2
, the first sliceselectdata
checks is A(:,1)
. By default,selectdata
uses the first dimension whose size is not equal 1.
Example: B = selectdata(A,pred,dim=2)
Postprocessing function to apply to the entries of the selected array, specified as a function handle. The postprocessing function must take one scalar input and return one scalar output. By default, selectdata
does not postprocess the selection from the array.
Example: B = selectdata(A,pred,postprocess=@(x)2*x)
Output Arguments
Selected array, returned as a vector or an array. The selected array has the data type outputted by the postprocessing function.
Indices of selected slices, returned as a vector. The indices are along the dimension dim of the selected slices. For instance, ifA is two-dimensional and dim
is2
, then `A`(:,I)
contains the selected slices.
Version History
Introduced in R2025a