selectdata
Description
specifies options using one or more name-value arguments in addition to the input arguments
in the previous syntax. For example, specify the B
= selectdata(___,Name=Value
)dim
dimension to
select in the input array.
Examples
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
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 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
Selected array, returned as a vector or an array. The selected array has the data type outputted by the postprocessing function.
Version History
Introduced in R2025a
See Also
Functions
Objects
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)