strncmp — Compare text inputs for case-sensitive equality up to N leading characters with MATLAB-compatible broadcasting.

strncmp(A, B, N) compares corresponding text elements and returns logical true when the first N characters match exactly. It follows MATLAB-compatible case-sensitive comparison and implicit expansion behavior.

Syntax

tf = strncmp(A, B, N)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesFirst text input (string/char/cell/string array).
BAnyYesSecond text input (string/char/cell/string array).
NIntegerScalarYesPrefix length to compare.

Returns

NameTypeDescription
tfLogicalArrayLogical prefix-comparison result.

Errors

IdentifierWhenMessage
RunMat:strncmp:InvalidInputAt least one text input is not a supported text container.strncmp: text inputs must be string/char/cell/string-array values
RunMat:strncmp:ShapeMismatchText inputs are not broadcast-compatible.strncmp: input sizes are not broadcast-compatible
RunMat:strncmp:InvalidPrefixLengthPrefix length argument is not a finite nonnegative integer scalar.strncmp: prefix length must be a finite nonnegative integer scalar
RunMat:strncmp:InternalErrorInternal logical result assembly failed.strncmp: internal error

How strncmp works

  • Accepted text types: String scalars/arrays, character vectors or character arrays, and cell arrays of character vectors.
  • Scalar N requirement: The third argument must evaluate to a finite, nonnegative integer scalar. Numeric, logical, and scalar tensor/logical-array values are accepted when they convert cleanly.
  • Implicit expansion: Scalars expand to match the size of the other operand before comparison.
  • Character arrays: Rows are treated as independent character vectors. Each row is compared against the other operand and the result is returned as a column vector.
  • Unicode-aware comparisons: Prefixes are counted in MATLAB characters (Unicode scalar values), so multi-byte UTF-8 sequences are handled transparently.
  • Prefix length semantics: If N is 0, every comparison evaluates to true. If either text element is shorter than N, it must match exactly up to the end of the shorter value and the longer value must also end within N characters to be considered equal.
  • Missing strings: Any comparison involving a missing string returns false unless N == 0.
  • Result type: Scalar comparisons return logical scalars. Array comparisons return logical arrays that follow MATLAB's column-major ordering.

Does RunMat run strncmp on the GPU?

strncmp is registered as an acceleration sink. When any operand resides on the GPU, RunMat gathers all inputs back to host memory before performing the comparison so that the behaviour matches MATLAB exactly. The logical result is always returned on the host. Providers do not need to supply specialised kernels.

GPU memory and residency

No. If you pass GPU-resident data, RunMat automatically gathers it to host memory before running strncmp. The builtin is an acceleration sink and always returns host logical outputs. Explicit gpuArray / gather calls are only required for compatibility with legacy MATLAB workflows.

Examples

Checking whether two strings share a prefix

tf = strncmp("RunMat", "Runway", 3)

Expected output:

tf = logical
   1

Comparing string arrays with implicit expansion

names = ["north" "south" "east"];
tf = strncmp(names, "no", 2)

Expected output:

tf = 1×3 logical array
   1   0   0

Comparing rows of a character array

animals = char("cat", "camel", "cow");
tf = strncmp(animals, "ca", 2)

Expected output:

tf = 3×1 logical array
   1
   1
   0

Comparing cell arrays element-wise

C1 = {'red', 'green', 'blue'};
C2 = {'rose', 'grey', 'black'};
tf = strncmp(C1, C2, 2)

Expected output:

tf = 1×3 logical array
   1   0   0

Handling zero-length comparisons

tf = strncmp("alpha", "omega", 0)

Expected output:

tf = logical
   1

Using strncmp with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how strncmp changes the result.

Run a small strncmp example, explain the result, then change one input and compare the output.

FAQ

What argument types does strncmp accept?

String arrays, character vectors/arrays, and cell arrays of character vectors. Mixed combinations are converted automatically. The third argument N must be a nonnegative integer scalar.

Is the comparison case-sensitive?

Yes. Use strncmpi if you need a case-insensitive prefix comparison.

What happens when N is zero?

The builtin returns true for every element because zero leading characters are compared.

How are shorter strings handled when N is larger than their length?

The shorter value must match the longer value exactly for its entire length, and the longer value must not have additional characters within the first N positions. Otherwise the comparison returns false.

How are missing string values treated?

Any comparison that involves a missing string returns false, except when N is zero (because no characters are compared).

Does strncmp produce logical results?

Yes. Scalar comparisons yield logical scalars; array inputs produce logical arrays that follow MATLAB’s column-major ordering.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how strncmp is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.