String vs character array in MATLAB - Performance Comparison
We want to understand how working with strings and character arrays affects the time it takes to run code in MATLAB.
How does the choice between string and character array change the speed of common operations?
Analyze the time complexity of concatenating characters to build a text.
str = "";
for k = 1:n
str = str + "a";
end
charArr = '';
for k = 1:n
charArr = [charArr 'a'];
end
This code builds a string and a character array by adding one character at a time in a loop.
Both loops run from 1 to n, repeating the concatenation operation.
- Primary operation: Adding one character to the existing text each time.
- How many times: Exactly n times for both string and character array.
Each time we add a character, MATLAB creates a new text by copying the old one and adding the new character.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 55 copies (1+2+...+10) |
| 100 | About 5050 copies |
| 1000 | About 500,500 copies |
Pattern observation: The total work grows roughly like the square of n because each addition copies all previous characters.
Time Complexity: O(n^2)
This means the time to build the text grows much faster than the number of characters added, because each step copies all existing characters.
[X] Wrong: "Adding one character in a loop always takes the same small time, so total time is O(n)."
[OK] Correct: Each addition copies the entire existing text, so the time grows with the length of the text, making the total time much larger.
Understanding how string building works helps you write faster code and shows you know how data structures affect performance.
"What if we used a pre-allocated character array and filled it instead of concatenating? How would the time complexity change?"
Practice
Solution
Step 1: Understand string syntax in MATLAB
Strings in MATLAB are enclosed in single quotes, like 'Hello World'.Step 2: Identify string and character array syntax
In MATLAB, single quotes denote character arrays (which are often used as strings). Double quotes denote string arrays introduced in newer MATLAB versions, but the classic string representation is single quotes.Final Answer:
'Hello World' -> Option AQuick Check:
Single quotes denote character arrays (strings) = B [OK]
- Confusing single quotes for strings
- Using curly braces which create cell arrays
- Using parentheses which are invalid for strings
Solution
Step 1: Recognize character array syntax
Character arrays are created using single quotes around text, like 'abc'.Step 2: Differentiate from string and cell array
Double quotes create string arrays, and curly braces create cell arrays, so only single quotes create character arrays.Final Answer:
charArray = 'abc'; -> Option DQuick Check:
Character arrays use single quotes = C [OK]
- Using double quotes for character arrays
- Using curly braces which create cell arrays
- Using square brackets with commas incorrectly
str = "MATLAB"; charArr = 'MATLAB'; disp(class(str)); disp(class(charArr));
Solution
Step 1: Identify variable types
Variablestris created with double quotes, so it is a string type. VariablecharArruses single quotes, so it is a character array (char).Step 2: Understand
Theclass()outputclass()function returns the type name as a string. Soclass(str)returns 'string' andclass(charArr)returns 'char'.Final Answer:
string char -> Option AQuick Check:
Double quotes = string, single quotes = char [OK]
- Assuming both are 'char' type
- Confusing output order
- Thinking class() returns variable content
str = 'Hello'; str(1) = "h";
What is the error in this code?
Solution
Step 1: Identify variable type
Variablestris created with single quotes, so it is a character array.Step 2: Understand assignment rules for character arrays
Character array elements must be assigned single characters using single quotes. Assigning a double-quoted string "h" causes a type mismatch error.Final Answer:
Cannot assign double quotes to a character array element -> Option BQuick Check:
Character arrays need single quotes for elements [OK]
- Using double quotes to assign char array elements
- Thinking MATLAB indexing starts at 0
- Assuming strings can't be indexed
Solution
Step 1: Understand types of inputs
"Data" is a string. 'Science' is a character array. To concatenate strings, both must be string type.Step 2: Convert character array to string before concatenation
Usingstring('Science')converts the char array to string, allowing + operator to concatenate strings properly.Final Answer:
result = "Data" + string('Science'); -> Option CQuick Check:
Convert char to string before + concatenation [OK]
- Trying to add string and char directly
- Using + with char arrays (causes error)
- Not converting char to string before concatenation
Ready to prove it?
Pass the test and this pattern moves unknown
