Main Content

selectdata

Select slices of arrays and generate CUDA code

Since R2025a

    Description

    B = selectdata(A,pred) returns an array with the slices of A that cause the predicate function pred to return true.

    example

    B = selectdata(___,Name=Value) 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.

    example

    [B,I] = selectdata(___) also returns the indices, I, of the selected slices.

    Examples

    collapse all

    Create a function removeZeros that uses selectData 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 vector u.

    u = [1 0 2 0 3 0 4 0 5 0 6 0];
    v = removeZeros(u)
    v = 1×6
         1     2     3     4     5     6

    Create a GPU code configuration object and generate code from removeZeros.

    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. Call selectdata with dim set to 2 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 from selectOddIndicesInRange.

    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 the uint32 data type. Select the RGB coordinates from A.

    B = selectRGB(A)
    
    B =
    
      2×3 uint32 matrix
    
        42   197   228
       126    10   223

    Create a GPU code configuration object and generate code from selectRGB.

    cfg = coder.gpuConfig("mex");
    codegen selectRGB -args {A} -config cfg;

    Input Arguments

    collapse all

    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 array slice. The function takes each slice by slicing A into arrays along the dimension dim. For example, if dim is 1, and A is a 3-by-5 array, the predicate function takes the 1-by-5 slices A(1,:), A(2,:), and A(3,:).

    • tf = pred(slice,idx) also accepts the index, idx, that corresponds to the slice. For example, if A is a three-dimensional array and dim is 2, then the input slice is equal to A(:,idx,:).

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=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 and dim is 2, the first slice selectdata 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

    collapse all

    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, if A is two-dimensional and dim is 2, then A(:,I) contains the selected slices.

    Version History

    Introduced in R2025a