ENGR113 - Lecture 5-Creating Arrays-B
ENGR113 - Lecture 5-Creating Arrays-B
ENGR 113
Creating Arrays-B
(Textbook Chapter 2)
Spring 2022
Instructor Name
1
Built-in Functions for Handling Arrays
MATLAB has many built-in functions for working with arrays. Some
common ones are:
• length(v): number of elements in a vector.
• size(A): number of rows and columns in a matrix or vector.
• reshape(A,m,n): changes number of rows and columns of a
matrix or vector while keeping total number of elements the same.
For example, changes a 4x4 matrix to a 2x8 matrix.
For more functions, use Help to search for the section labeled "Matrices
and Arrays“.
L = length(X) returns the length of the largest array dimension in X.
The length of an empty array is zero.
Create a random matrix and separately return number of rows & columns.
>> A = rand(4,3);
How can we create a random
>> [numRows, numCols] = size(A) matrix of integers?
numRows = 4
numCols = 3
The reshape function changes the size and shape of an array, as long as
the number of elements in each shape are the same.
MATLAB functions that accept character arrays as inputs also accept string
arrays, and vice versa.
• For a full listing of functions used to manipulate text in character arrays
and string arrays, visit: mathworks.com/help/matlab/characters-and-
strings.html
Character Arrays
A character array is a sequence of characters, just as a numeric array is a
sequence of numbers.
• The characters are stored in the array just like numbers.
• Each character in the array, including a space, is an element with index.
• A one-line character array is a row vector.
You can concatenate (add text to the end of) a character vector with
square brackets, just as you concatenate other types of arrays.
>> seq = [seq, 'ATTAGAAACC']
Two more equivalent ways:
seq = 'GCTAGAATCCATTAGAAACC'
seq(end+1:end+10) = 'ATTAGAAACC'
seq = append(seq, 'ATTAGAAACC')
>> name = 'Howard the Duck';
>> size(name)
ans =
1 15 Number of elements is number of characters in the vector
>> x = 356
x =
356
>> y = '356'
y =
'356'
You can create a string array by enclosing a piece of text in double quotes.
>> str = "Hello, world"
Try to find:
str = size(str)
"Hello, world"
Though the text "Hello, world" is 12-character long, str itself is a 1-
by-1 string. You can use a string to specify a file name, plot label, or any
other piece of textual information.
You can find the number of characters in each element of a string using the
strlength function.
>> n = strlength(str)
strlength accepts both
n = 12 character and string arrays
If the text itself includes double quotes, use two double quotes within the
definition.
>> str = "They said, ""Welcome!"" and waved."
str =
"They said, "Welcome!" and waved."
To create a 2D string array, you can use square brackets.
>> str = ["Mercury" "Gemini" "Apollo"; Notice the use of spaces and
"Skylab" "Skylab B" "ISS"] semicolons
str =
2×3 string array
Try to find:
"Mercury" "Gemini" "Apollo" size(str)
"Skylab" "Skylab B" "ISS" strlength(str)
>> name = "Ali "; Recall that the append function was also
surname = "Ahmed"; used with character arrays
fullname = append(name, surname)
fullname =
"Ali Ahmed"
We can find and replace all occurrences of a substring using the function
strrep.
Here, we have replaced all occurrences of “good” with “great” in the string
claim and stored the output in a new string named new_claim
The function strfind(TEXT,PATTERN) returns the starting indices of
any occurrences of PATTERN in the string TEXT.
20