What does the strcmpi function do in MATLAB / RunMat?
strcmpi(a, b) compares text values without considering letter case. It returns logical true when inputs match case-insensitively and false otherwise. Supported text types mirror MATLAB: string scalars/arrays, character vectors and arrays, and cell arrays filled with character vectors.
How does the strcmpi function behave in MATLAB / RunMat?
- Case-insensitive: Letter case is ignored.
"RunMat","runmat", and"RUNMAT"all compare equal. - Accepted text types: Works with string arrays, character vectors or matrices, and cell arrays of character vectors. Mixed combinations are normalised automatically.
- Implicit expansion: Scalar operands expand against array operands so element-wise comparisons follow MATLAB broadcasting rules.
- Character arrays: Rows are compared individually. Results are column vectors whose length equals the number of rows in the character array.
- Cell arrays: Each cell is treated as a text scalar. Scalar cells expand across the other operand before comparison.
- Missing strings: Elements whose value is
missing("<missing>") always compare unequal, even to other missing values, matching MATLAB. - Result form: Scalar comparisons return logical scalars. Otherwise the result is a logical array that matches the broadcast shape.
strcmpi Function GPU Execution Behaviour
strcmpi is registered as an acceleration sink. When either operand resides on the GPU, RunMat gathers both to host memory before comparing them. This keeps behaviour identical to MATLAB and avoids requiring backend-specific kernels. The logical result is produced on the CPU and never remains GPU-resident.
Examples of using the strcmpi function in MATLAB / RunMat
Compare Two Strings Ignoring Case
tf = strcmpi("RunMat", "runmat");
Expected output:
tf = logical
1
Find Case-Insensitive Matches Inside a String Array
colors = ["Red" "GREEN" "blue"];
mask = strcmpi(colors, "green");
Expected output:
mask = 1×3 logical array
0 1 0
Compare Character Array Rows Without Case Sensitivity
animals = char("Cat", "DOG", "cat");
tf = strcmpi(animals, "cAt");
Expected output:
tf = 3×1 logical array
1
0
1
Compare Cell Arrays Of Character Vectors Case-Insensitively
C1 = {'north', 'East', 'SOUTH'};
C2 = {'NORTH', 'east', 'west'};
tf = strcmpi(C1, C2);
Expected output:
tf = 1×3 logical array
1 1 0
Broadcast A String Scalar Against A Character Matrix
patterns = char("alpha", "BETA");
tf = strcmpi(patterns, ["ALPHA" "beta"]);
Expected output:
tf = 2×2 logical array
1 0
0 1
Handle Missing Strings In Case-Insensitive Comparisons
values = ["Active" missing];
mask = strcmpi(values, "active");
Expected output:
mask = 1×2 logical array
1 0
GPU residency in RunMat (Do I need gpuArray?)
You rarely need to call gpuArray manually. When inputs already live on the GPU, RunMat gathers them before calling strcmpi, then returns a logical result on the host. This matches MATLAB’s behaviour while keeping the runtime simple. Subsequent GPU-aware code can explicitly transfer results back if needed.
FAQ
Does strcmpi support string arrays, character arrays, and cell arrays?
Yes. All MATLAB-supported text containers are accepted, and mixed combinations are handled automatically with implicit expansion.
Is whitespace significant when comparing character arrays?
Yes. Trailing spaces or different lengths make rows unequal, just like strcmp. Use strtrim or strip if you need to ignore whitespace.
Do missing strings compare equal?
No. The missing sentinel compares unequal to all values, including another missing string scalar.
Can I compare complex numbers or numeric arrays with strcmpi?
No. Both arguments must contain text. Numeric inputs produce a descriptive MATLAB-style error.
How are GPU inputs handled?
They are gathered to the CPU automatically before comparison. Providers do not need to implement additional kernels for strcmpi.
What is returned when both inputs are scalars?
A logical scalar is returned (true or false). For non-scalar shapes, a logical array that mirrors the broadcast dimensions is produced.
See Also
strcmp, contains, startswith, endswith, strip
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/strings/core/strcmpi.rs - Found a bug? Please open an issue with a minimal reproduction.