York University Itec2600 by Amanda Tian
York University Itec2600 by Amanda Tian
Example about defining string vector/array. Note that when defining a string vector we should use double quotation
“”,
s=
>> size(s)
ans =
1 4
>> whos s
Compare with the above example s3 = ['my' ' name' ' is' ' peter'], we see the structure of a character vector and a
string vector is different.
>>s3(1) % the first element of a character vector
ans =
'm'
ans =
"my"
ans =
16
ans =
s=
4×1 string array
York University ITEC2600 By Amanda Tian
"my"
"name"
"is"
"peter"
>> size(s)
ans =
4 1
>> whos s
>> s(1)
ans =
"my"
Examples about defining string cell array. It is defined using single quotation ‘’ and {}.
s=
s=
'my'
'name'
'is'
'peter'
ans =
4 1
>> whos s
Name Size Bytes Class Attributes
>> s(1)
ans =
cell
York University ITEC2600 By Amanda Tian
'my'
A cell array is a data type with indexed data containers called cells. Each cell can contain any type of data. Cell
arrays commonly contain pieces of text, combinations of text and numbers from spreadsheets or text files, or
numeric arrays of different sizes.
There are two main difference between vector/matrix and cell array:
• All elements in a vector/matrix have to be the same data type. While for cell array, the elements can be
different data type
• For vector/matrix, the object on each location must be a single value, but for cell array, it elements can be
another object which contain multiple values.
Examples about creating cell array, note the {} is for creating cell, [] is for creating vector:
C=
2×3 cell array
[] [] []
[] [] []
>> C1 = {4, 5, 6; 'text', [1 2 3], ['t1','t2']} % a 2 by 3 cell array, the last element is a character vector.
C1 =
2×3 cell array
>> C2 = {4, 5, 6; 'text', [1 2 3], {'t1','t2'} } % similar to above code, the last element is a string cell array
C2 =
2×3 cell array
>> C3 = {4, 5, 6; 'text', [1 2 3], ["t1","t2"]} % similar to above code, the last element is a string vector
C 3=
>> C1(1,2) % return the cell of first row and 2nd column
ans =
cell
York University ITEC2600 By Amanda Tian
[2]
>> C1{1,2} % the content within the cell of 1st row and 2nd column
ans =
ans =
'cell'
ans =
'double'
>> C1(2,2) % return the cell of 2nd row and 2nd column
ans =
cell
[1×3 double]
>> C1{2,2} % the content within the cell of 2nd row and 2nd column
ans =
1 2 3
>> class(C1(2,2)) % check the object type of C(2, 2), which is a cell
ans =
'cell'
>> class(C1{2,2}) % check the object type of C{2, 2}, which is a numeric vector
ans =
'double'
If we want to view the 2nd level elements, for example, we want to see ‘t1’ in the 2nd row 3rd column of the cell, it
depends on the data type of the 1st level element. See examples:
>> C1{2,3}(1:2) % for C1 the 2nd row 3rd column is a character vector
ans =
't1'
>> C2{2,3}{1} % for C2 the 2nd row 3rd column is a string cell
ans =
't1'
>> C3{2,3}(1) % for C3 the 2nd row 3rd column is a string vector
York University ITEC2600 By Amanda Tian
ans =
"t1"
c=
1×4 cell array
c=
1×5 cell array
s=
'Goodmorning'
s=
'Good,morning'
>> s = strcat(s1,' ' , s2) % add space between s1, s2, ' ' does not work
s=
'Goodmorning'
>> s = strcat(s1," " , s2) % add space between s1, s2,should use " "
s=
"Good morning"
str =
1×2 string array
>> str = strcat(s1,": ",s2) % paste two string vectors, put colon ':' as separator
York University ITEC2600 By Amanda Tian
str =
1×2 string array
s=
'1000'
s=
'10.234'
n=
99.9
n=
50
>> class = 'grade two' % a string variable with the grade information
class =
'grade two'
score =
66.7
We can attach the strings and variables together. Note that %d: integer; %f: decimal/numerical; %s: string in the
following example.
>> str = sprintf("There are %d studens in the %s, the average score is %f", n,class,score)
str =
"There are 50 studens in the grade two, the average score is 66.700000"
Example 2:
what about to generate multiple strings with the same pattern. Note that this example shows how to deal with vectors
in sprintf. This part is not requested in exam.
n=
50 40 60
>> class ={ 'grade one' 'grade two' 'grade three'} % a string cell with the grade information
class =
1×3 cell array
York University ITEC2600 By Amanda Tian
>> score = [ 66.7 69.8 70.25] % a numerical vector with average score
score =
66.7 69.8 70.25
>> v = [num2cell(n); class; num2cell(score)] % a 3 by 3 cell including all information. [] is for vector, since the
data type are different, so it forced to a cell.
v=
3×3 cell array
v1 =
3×1 cell array
{1×3 cell}
{1×3 cell}
{1×3 cell}
The following code can create multiple lines with multiple values. Note v{:} is Comma Separated List (CSL), it lists
all elements in v.
>> str = sprintf("There are %d studens in the %s, the average score is %.2f \n", v{:})
str =
"There are 50 studens in the grade one, the average score is 66.70
There are 40 studens in the grade two, the average score is 69.80
There are 60 studens in the grade three, the average score is 70.25
"
Below is one example of creating a table with four columns: LastName (string), Age (integers), Smoker (logical),
Height (decimal/numerical). Note that if we define a vector for a column of a table, then the vector need to be a
column vector.
LastName =
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
Age =
38
43
38
40
49
Smoker =
5×1 logical array
1
0
1
0
1
Height =
171.0000
69.5000
64.3000
67.0000
64.0000
T=
5×4 table
In the following example, I want to show that a cell array can also be assigned to column of table. But usually
people use vector, in which the data structure is more organized, and it easier to analysis.
LastName1 =
'Sanchez'
'Johnson'
'Li'
'Diaz'
{1×2 cell}
T1 =
5×4 table
ans =
2×4 table
Note that all the following six example are equivalent, they all return the first column of the table.
ans =
5×1 table
LastName
_________
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T(:,'LastName') % column subscript is a character vector, equivalent to above, , output is a table
ans =
5×1 table
York University ITEC2600 By Amanda Tian
LastName
_________
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T{:,1} % using {}, the output is a string vector other than a table
ans =
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T{:,'LastName'} % using {}, the output is a string vector other than a table
ans =
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
ans =
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T.(1) % view a column by column index ( note '.' and ())
ans =
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
In the following examples, we take the 2nd and 3rd elements of the ‘LastName’ column. We can use different way to
get it.
>> T([2,3],1)
York University ITEC2600 By Amanda Tian
ans =
2×1 table
LastName
_________
"Johnson"
"Li"
>> T([2,3],'LastName')
ans =
2×1 table
LastName
_________
"Johnson"
"Li"
>> T{[2,3],1} % using {}, similar with above, but the output type is different
ans =
2×1 string array
"Johnson"
"Li"
>> T{[2,3],'LastName'} % using {}, similar with above, but the output type is different
ans =
2×1 string array
"Johnson"
"Li"
>> T.LastName([2,3])
ans =
2×1 string array
"Johnson"
"Li"
>> T.(1)([2,3])
ans =
2×1 string array
"Johnson"
"Li"
ans =
3×1 table
LastName
________
York University ITEC2600 By Amanda Tian
"Li"
"Diaz"
"Brown"
ans =
"Li"
"Diaz"
"Brown"
ans =
"Li"
"Diaz"
"Brown"
View multiple columns of a table. Similarly, there are multiple ways to do that.
ans =
5×2 table
LastName Age
_________ ___
"Sanchez" 38
"Johnson" 43
"Li" 38
"Diaz" 40
"Brown" 49
>> T(:,[1,2])
ans =
5×2 table
LastName Age
_________ ___
"Sanchez" 38
"Johnson" 43
"Li" 38
"Diaz" 40
"Brown" 49
>> T{:,{'LastName', 'Age'}} % using {}, similar with above, but the output type is different
ans =
5×2 string array
"Sanchez" "38"
"Johnson" "43"
"Li" "38"
York University ITEC2600 By Amanda Tian
"Diaz" "40"
"Brown" "49"
>> T{:,[1,2]} % using {}, similar with above, but the output type is different
ans =
"Sanchez" "38"
"Johnson" "43"
"Li" "38"
"Diaz" "40"
"Brown" "49"
>> T(:,1:2)
ans =
5×2 table
LastName Age
_________ ___
"Sanchez" 38
"Johnson" 43
"Li" 38
"Diaz" 40
"Brown" 49
>> T(:,{'LastName','Age'})
ans =
5×2 table
LastName Age
_________ ___
"Sanchez" 38
"Johnson" 43
"Li" 38
"Diaz" 40
"Brown" 49
We can also put conditions to filter the subset of a table, see example below, we need a subset in which ‘Age’ is
greater or equal to 40. Note that this logical condition is selecting subset by rows, although the condition itself is
about a column (Age).
>> T(T.Age >= 40 , :) % get a subset by condition expression (logicals)
ans =
3×4 table
York University ITEC2600 By Amanda Tian
ans =
3×1 table
Age
___
43
40
49
>> T(T.Age >= 40, 1 ) % get a subset by condition expression (logicals), only take the first column
ans =
3×1 table
LastName
_________
"Johnson"
"Diaz"
"Brown"
nrow =
5
ncol =
4
ans =
5
ans =
4
>> T = table(LastName,Age,Smoker,Height)
T=
5×4 table
T=
4×4 table
T=
4×3 table
"Johnson" 43 false
"Li" 38 true
"Diaz" 40 false
"Brown" 49 true
T=
4×3 table
"NewLastName" 43 false
"Li" 38 true
"Diaz" 40 false
"Brown" 49 true
>> T.LastName(2) = {'NewLastName1'} % change 2nd element of "LastName' column. Note the {} and ()
T=
York University ITEC2600 By Amanda Tian
4×3 table
"NewLastName" 43 false
"NewLastName1" 38 true
"Diaz" 40 false
"Brown" 49 true
>> T(1,2) = {10} % another example. Note: an assignment into a table must be another table or a cell array.
T=
4×3 table
"NewLastName" 10 false
"NewLastName1" 38 true
"Diaz" 40 false
"Brown" 49 true
T=
5×4 table
>> T.newcol = [1,2,3,4, 5]' % add one new column to table, column name is ‘newcol’
T=
5×5 table
LastName Age Smoker Height newcol
_________ ___ ______ ______ ______
>> T = [T; {"LN", 55, 0, 80.6 6}] % add one new row to table
T=
6×5 table
"Sanchez" 38 1 171 1
"Johnson" 43 0 69.5 2
York University ITEC2600 By Amanda Tian
"Li" 38 1 64.3 3
"Diaz" 40 0 67 4
"Brown" 49 1 64 5
"LN" 55 0 80.6 6
The following is an example to add a vector to a table, but get error, even if the table has all column the same data
type (integers)
>> a = [1 2 3]'
a=
1
2
3
>> b = [4 5 6]'
b=
4
5
6
>> c = [7 8 9]'
c=
7
8
9
>> TT = table(a, b, c)
TT =
3×3 table
a b c
_ _ _
1 4 7
2 5 8
3 6 9
7. Logical operation.
Examples of logical operations:
logical
1
>> a <= b % a less than or equal to b?
ans =
logical
1
>> a > b % a is greater than b? ?
ans =
logical
0
>> a >= b % a is not greater than or exactly equal to b?
ans =
logical
0
The following two example are different: a=2 is assigning value to a; a == 2 is checking if a=2.
>> a == 2 % a exactly equal to 2? Note it is == instead of =
ans =
logical
1
>> a == 3 % a is not exactly equal to 3? Note it is == instead of =
ans =
logical
0
>> true + 2 % true = 1
ans =
3
>> false + 2 % false = 0
ans =
2
~: ‘not’ logical operator
~A returns a logical array of the same size as A. The array contains logical 1 (true) values where A is zero and
logical 0 (false) values where A is nonzero.
>> ~true % NOT true = false
ans =
logical
0
>> a==2
ans =
logical
1
>> ~ a==2 % opposite to a == 2
ans =
York University ITEC2600 By Amanda Tian
logical
0
>> A = eye(3) % 3 by 3 identity matrix
A=
1 0 0
0 1 0
0 0 1
>> B = ~A % not A
B=
3×3 logical array
0 1 1
1 0 1
1 1 0
>> A = [5 7 0; 0 2 9; 5 0 0] % all non-zero values are taken as 1.
A=
5 7 0
0 2 9
5 0 0
>> B = ~ A % not A
B=
3×3 logical array
0 0 1
1 0 0
0 1 1
ans =
2×4 logical array
1 0 0 0
0 0 0 1
>> A = [5 7 0; 0 2 9; 5 0 0]
A=
5 7 0
0 2 9
5 0 0
>> B = [6 6 0; 1 3 5; -1 0 0]
B=
6 6 0
1 3 5
-1 0 0
>> A & B % elements level 'and', non-zero is taken as 1.
ans =
3×3 logical array
1 1 0
0 1 1
1 0 0
>> A = [true false]
A=
1×2 logical array
1 0
>> B = [true; false]
B=
2×1 logical array
1
0
>> C = A & B % row vector & column vector results in a matrix
C=
2×2 logical array
1 0
0 0
|: OR logical operator
A | B performs a logical OR of arrays A and B and returns an array containing elements set to either logical 1 (true)
or logical 0 (false). An element of the output array is set to logical 1 (true) if either A or B contain a nonzero element
at that same array location. Otherwise, the array element is set to 0.
1
>> a == 3 | b==2 % is true because both are false
ans =
logical
0
>> [1 0 1 0] | [1 1 0 0] % | can be applied to vectors, elements level operation
ans =
1×4 logical array
1 1 1 0
>> [1 0 1 0; 0 0 0 1] | [1 1 0 0; 1 0 1 1] % | can be applied to vectors, elements level operation
ans =
2×4 logical array
1 1 1 0
1 0 1 1
>> A = [5 7 0; 0 2 9; 5 0 0]
A=
5 7 0
0 2 9
5 0 0
>> B = [6 6 0; 1 3 5; -1 0 0]
B=
6 6 0
1 3 5
-1 0 0
>> A | B % elements level 'or', non-zero is taken as 1.
ans =
3×3 logical array
1 1 0
1 1 1
1 0 0
>> A = [true false]
A=
1×2 logical array
1 0
>> B = [true; false]
B=
2×1 logical array
1
0
>> C = A|B % row vector | column vector results in a matrix
C=
2×2 logical array
1 1
1 0