What does the strcmp function do in MATLAB / RunMat?
strcmp(a, b) returns logical true when two pieces of text match exactly and false otherwise.
It accepts string arrays, character vectors/arrays, and cell arrays of character vectors, mirroring MATLAB semantics.
How does the strcmp function behave in MATLAB / RunMat?
- Accepted text types: Works with string scalars/arrays, character vectors or matrices created with
char, and cell arrays of character vectors. Mixed combinations are converted automatically, matching MATLAB. - Implicit expansion: Scalar inputs expand to the shape of the other operand, producing element-wise comparisons for higher-dimensional arrays.
- Character arrays: Rows are compared individually. The result is a column vector whose length equals the number of rows in the character array.
- Cell arrays: Each cell is treated as a text scalar. Scalar cell arrays expand across the other operand before comparison.
- Missing strings: String elements equal to
missingcompare unequal to everything, including othermissingvalues. - Result form: A single logical scalar is returned for scalar comparisons; otherwise you receive a logical array using column-major MATLAB layout.
- Case sensitivity: Matching is case-sensitive. Use
strcmpifor case-insensitive comparisons.
strcmp Function GPU Execution Behaviour
strcmp is registered as an acceleration sink. When either input resides on the GPU, RunMat gathers both
operands back to host memory before comparing them so the results match MATLAB exactly. Providers do not
need to implement custom kernels, and the logical result is always returned on the CPU.
Examples of using the strcmp function in MATLAB / RunMat
Compare Two Equal Strings
tf = strcmp("RunMat", "RunMat");
Expected output:
tf = logical
1
Compare String Array With Scalar Text
names = ["red" "green" "blue"];
tf = strcmp(names, "green");
Expected output:
tf = 1×3 logical array
0 1 0
Compare Character Array Rows
labels = char("cat", "dog", "cat");
tf = strcmp(labels, "cat");
Expected output:
tf = 3×1 logical array
1
0
1
Compare Two Cell Arrays Of Character Vectors
C1 = {'apple', 'pear', 'grape'};
C2 = {'apple', 'peach', 'grape'};
tf = strcmp(C1, C2);
Expected output:
tf = 1×3 logical array
1 0 1
Handle Missing Strings
vals = ["alpha" missing];
tf = strcmp(vals, "alpha");
Expected output:
tf = 1×2 logical array
1 0
Implicit Expansion With Column Vector Text
patterns = char("north", "south");
tf = strcmp(patterns, ["north" "east"]);
Expected output:
tf = 2×2 logical array
1 0
0 0
GPU residency in RunMat (Do I need gpuArray?)
You normally do not need to call gpuArray. If you do, RunMat gathers the operands before computing strcmp
so the output matches MATLAB. The result always lives on the host because this builtin inspects text data.
FAQ
What types can I pass to strcmp?
Use string arrays, character vectors/arrays, or cell arrays of character vectors. Mixed combinations are accepted and follow MATLAB's implicit expansion rules.
Does strcmp ignore letter case?
No. strcmp is case-sensitive. Use strcmpi for case-insensitive comparisons.
What happens when the inputs contain missing strings?
Missing string scalars compare unequal to every value (including other missing strings), so the result is false.
Can strcmp compare matrices of characters?
Yes. Character arrays compare row-by-row, returning a column vector whose entries tell you whether each row matches.
Does strcmp return numeric or logical results?
It returns logical results. Scalars become logical scalars (Value::Bool), while arrays are returned as logical arrays.
See Also
string, char, contains, startswith, strlength
Source & Feedback
- Implementation:
crates/runmat-runtime/src/builtins/strings/core/strcmp.rs - Found a bug? Please open an issue with a minimal reproduction.