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 = ["my" "name" "is" "peter"] % a string vector/array, row vector.
s=
1×4 string array
"my" "name" "is" "peter"
>> size(s)
ans =
1 4
>> whos s
Name Size Bytes Class Attributes
s 1x4 320 string
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'
>> s(1) % the first element of a string vector
ans =
"my"
>> length(s3) % length of a character vector
ans =
16
>> length(s) % length of a string vector
ans =
>> whos s3 % check the type of s3, it is ‘char’
Name Size Bytes Class Attributes
s3 1x16 32 char
>> whos s % check the type of s, it is ‘string’
Name Size Bytes Class Attributes
s 1x4 344 string
Example of column string vector:
>> s = ["my"; "name"; "is"; "peter"] % a string vector/array, column vector.
s=
4×1 string array
York University ITEC2600 By Amanda Tian
"my"
"name"
"is"
"peter"
>> size(s)
ans =
4 1
>> whos s
Name Size Bytes Class Attributes
s 4x1 320 string
>> s(1)
ans =
"my"
Examples about defining string cell array. It is defined using single quotation ‘’ and {}.
>> s = {'my' 'name' 'is' 'peter'} % a string cell array.
s=
1×4 cell array
'my' 'name' 'is' 'peter'
>> s = {'my'; 'name'; 'is'; 'peter'} % a string cell array, vertical
s=
4×1 cell array
'my'
'name'
'is'
'peter'
>> size(s) % check the size of s
ans =
4 1
>> whos s
Name Size Bytes Class Attributes
s 4x1 474 cell
>> s(1)
ans =
cell
York University ITEC2600 By Amanda Tian
'my'
5.2 cell array.
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 = cell(2, 3) % create a 2 by 3 empty cell.
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
{[ 4]} {[ 5]} {[ 6]}
{'text'} {1×3 double} {'t1t2'}
>> 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
{[ 4]} {[ 5]} {[ 6]}
{'text'} {1×3 double} {1×2 cell}
>> C3 = {4, 5, 6; 'text', [1 2 3], ["t1","t2"]} % similar to above code, the last element is a string vector
C 3=
2×3 cell array
{[ 4]} {[ 5]} {[ 6]}
{'text'} {1×3 double} {1×2 string}
There are two ways to refer to the elements of a cell array.
• Enclose indices in smooth parentheses, (), to refer to sets of cells — for example, to define a subset of the
array.
• Enclose indices in curly braces, {}, to refer to the text, numbers, or other data within individual cells.
See the following examples, we use class() function to check the type of the objects
>> 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 =
>> class(C1(1,2)) % check the object type of C(1, 2)
ans =
'cell'
>> class(C1{1,2}) % check the object type of C{1, 2}
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"
5.3 strsplit: split string at specified delimiter.
Note that the result is a cell.
>> s = 'The rain in Spain.'; % define a string variable
>> c = strsplit(s) % split the string words by space ' ', by default
c=
1×4 cell array
{'The'} {'rain'} {'in'} {'Spain.'}
>> data = '1.21, 1.985, 1.955, 2.015, 1.885'; % this is a string
>> c = strsplit(data,', ') % split the sting by ', '
c=
1×5 cell array
{'1.21'} {'1.985'} {'1.955'} {'2.015'} {'1.885'}
5.4 strcat: Concatenate/paste strings horizontally
Examples about pasting character vectors.
>> s1 = 'Good'; % a string variable
>> s2 = 'morning'; % a string variable
>> s = strcat(s1,s2) % paste two strings horizontally
s=
'Goodmorning'
>> s = strcat(s1,', ' , s2) % add , between s1, s2,
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"
Examples about pasting string vectors.
>> s1 = ["John ","Mary "]; % a string vector/array
>> s2 = ["M","f"]; % a string vector/array
>> str = strcat(s1,s2) % paste two string vectors
str =
1×2 string array
"John M" "Mary f"
>> str = strcat(s1,": ",s2) % paste two string vectors, put colon ':' as separator
York University ITEC2600 By Amanda Tian
str =
1×2 string array
"John : M" "Mary : f"
5.5 data type conversion
>> s = int2str(1000) % convert integer 1000 to string '1000'
s=
'1000'
>> s = num2str(10.234) % convert number 10.234 to string '10.234'
s=
'10.234'
>> n = str2num('99.9') % convert string '99.9' to number 99.9
n=
99.9
5.6 sprintf: format data to string.
More details please refer to https://www.mathworks.com/help/matlab/ref/sprintf.html
Example 1:
>> n = 50 % a integer variable with the number of students
n=
50
>> class = 'grade two' % a string variable with the grade information
class =
'grade two'
>> score = 66.7 % a numerical variable with average score
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] % a integer vector with the number of students
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
{'grade one'} {'grade two'} {'grade three'}
>> 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
{[ 50]} {[ 40]} {[ 60]}
{'grade one'} {'grade two'} {'grade three'}
{[ 66.7]} {[ 69.8]} {[ 70.25]}
>> v1 = {num2cell(n); class; num2cell(score)} % a 3 by 1 cell including all information
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
"
6. Create and view table
6.1 create a table.
Table is another data set object in Matlab, which is similar to matrix and cell. Table arrays store column-oriented or
tabular data, such as columns from a text file or spreadsheet. Tables store each piece of column-oriented data in
a variable. Table variables can have different data types and sizes as long as all variables have the same number of
rows.
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"] % string type column vector
York University ITEC2600 By Amanda Tian
LastName =
5×1 string array
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> Age = [38;43;38;40;49] % integer type
Age =
38
43
38
40
49
>> Smoker = logical([1;0;1;0;1]) % logical type
Smoker =
5×1 logical array
1
0
1
0
1
>> Height = [171; 69.5; 64.3; 67; 64] % numeric
Height =
171.0000
69.5000
64.3000
67.0000
64.0000
>> T = table(LastName,Age,Smoker,Height) % combine different type of data/columns to table
T=
5×4 table
LastName Age Smoker Height
_________ ___ ______ ______
"Sanchez" 38 true 171
"Johnson" 43 false 69.5
"Li" 38 true 64.3
"Diaz" 40 false 67
"Brown" 49 true 64
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'; {'Brown1','Brown2'}} % a string cell
LastName1 =
5×1 cell array
York University ITEC2600 By Amanda Tian
'Sanchez'
'Johnson'
'Li'
'Diaz'
{1×2 cell}
>> T1 = table(LastName1,Age,Smoker,Height) % the first column is a cell.
T1 =
5×4 table
LastName1 Age Smoker Height
__________ ___ ______ ______
'Sanchez' 38 true 171
'Johnson' 43 false 69.5
'Li' 38 true 64.3
'Diaz' 40 false 67
{1×2 cell} 49 true 64
6.2 view subset/elements in a table.
To index into a table, use smooth parentheses () to return a sub-table or curly braces {} to extract the contents. You
can reference variables and rows using names.
Examples:
>> T(2:3,:) % 2nd and 3rd rows, specified by row index
ans =
2×4 table
LastName Age Smoker Height
_________ ___ ______ ______
"Johnson" 43 false 69.5
"Li" 38 true 64.3
Note that all the following six example are equivalent, they all return the first column of the table.
>> T(:,1) % column subscript is a integer, output is a 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 =
5×1 string array
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T{:,'LastName'} % using {}, the output is a string vector other than a table
ans =
5×1 string array
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T.LastName % view a column by column name ( note '.')
ans =
5×1 string array
"Sanchez"
"Johnson"
"Li"
"Diaz"
"Brown"
>> T.(1) % view a column by column index ( note '.' and ())
ans =
5×1 string array
"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"
‘end’ can also be used to refer subset of a table.
>> T(3:end,'LastName') % the 'LastName' column, 3rd element until the last one
ans =
3×1 table
LastName
________
York University ITEC2600 By Amanda Tian
"Li"
"Diaz"
"Brown"
>> T.LastName(3:end) % equivalent to above.
ans =
3×1 string array
"Li"
"Diaz"
"Brown"
>> T.(1)(3:end) % equivalent to above.
ans =
3×1 string array
"Li"
"Diaz"
"Brown"
View multiple columns of a table. Similarly, there are multiple ways to do that.
>> T(:,{'LastName', 'Age'})
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 =
5×2 string array
"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
Below examples are incorrect.
>> T(:,["LastName","Age"]) % why error? - string vector can not be used to specify column index.
Table variable subscripts must be real positive integers, logicals, character vectors, or cell arrays of
character vectors.
>> T(:,['LastName','Age']) % a string can not be used to specify column index.
Unrecognized variable name 'LastNameAge'.
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
LastName Age Smoker Height
_________ ___ ______ ______
"Johnson" 43 false 69.5
"Diaz" 40 false 67
"Brown" 49 true 64
>> T(T.Age >= 40 , 'Age') % get a subset by condition expression (logicals), only take the ‘Age’ column.
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"
Example to get the dimension of a table:
>> [nrow ncol]= size(T) % can also use size() to view the dimension of a table.
nrow =
5
ncol =
4
>> height(T) % # of rows in table T
ans =
5
>> width(T) % # of columns in table T
ans =
4
6.3 save table to file.
Writetable() function can be used to save/export a table to file. The file can be .txt, .csv, xlsx,... You can also choose
to include the full path of the file or not. If there is no full path specified, the file will be saved into current folder.
writetable(T,'smoke.txt'); % output to txt file
writetable(T,'smoke.csv'); % output to csv file
writetable(T,'smoke.xlsx'); % output to xlsx file
writetable(T,'C:\Amanda\york\ITEC2600 - Introduction to Analytical Programming\note\output\smoke.txt'); %
output file with full path
6.4 change elements' value. remove or delete rows/columns
See examples:
York University ITEC2600 By Amanda Tian
>> T = table(LastName,Age,Smoker,Height)
T=
5×4 table
LastName Age Smoker Height
_________ ___ ______ ______
"Sanchez" 38 true 171
"Johnson" 43 false 69.5
"Li" 38 true 64.3
"Diaz" 40 false 67
"Brown" 49 true 64
>> T(1,:) = [] % delete first row
T=
4×4 table
LastName Age Smoker Height
_________ ___ ______ ______
"Johnson" 43 false 69.5
"Li" 38 true 64.3
"Diaz" 40 false 67
"Brown" 49 true 64
>> T(:,'Height') = [] % delete 'Height column
T=
4×3 table
LastName Age Smoker
_________ ___ ______
"Johnson" 43 false
"Li" 38 true
"Diaz" 40 false
"Brown" 49 true
>> T(1,1) = {'NewLastName'} % first row, first column changes.
T=
4×3 table
LastName Age Smoker
_____________ ___ ______
"NewLastName" 43 false
"Li" 38 true
"Diaz" 40 false
"Brown" 49 true
>> T(1,1) = 'NewLastName' % why error?
The number of table variables in an assignment must match.
>> T.LastName(2) = {'NewLastName1'} % change 2nd element of "LastName' column. Note the {} and ()
T=
York University ITEC2600 By Amanda Tian
4×3 table
LastName Age Smoker
______________ ___ ______
"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
LastName Age Smoker
______________ ___ ______
"NewLastName" 10 false
"NewLastName1" 38 true
"Diaz" 40 false
"Brown" 49 true
6.5 add row or column to table.
Note that when adding a new row to the table, the new row need to be defined as a cell.
>> T = table(LastName,Age,Smoker,Height)
T=
5×4 table
LastName Age Smoker Height
_________ ___ ______ ______
"Sanchez" 38 true 171
"Johnson" 43 false 69.5
"Li" 38 true 64.3
"Diaz" 40 false 67
"Brown" 49 true 64
>> 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
_________ ___ ______ ______ ______
"Sanchez" 38 true 171 1
"Johnson" 43 false 69.5 2
"Li" 38 true 64.3 3
"Diaz" 40 false 67 4
"Brown" 49 true 64 5
>> T = [T; {"LN", 55, 0, 80.6 6}] % add one new row to table
T=
6×5 table
LastName Age Smoker Height newcol
_________ ___ ______ ______ ______
"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
>> TTT = [ [0 0 0];TT ]
All input arguments must be tables.
7. Logical operation.
Examples of logical operations:
>> a = 2 % set 2 to variable a
a=
2
>> b = 3 % set 3 to variable b
b=
3
>> a < b % a less than b?
ans =
York University ITEC2600 By Amanda Tian
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
&: ADD logical operator
A & B performs a logical AND 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 both A and B contain a nonzero
element at that same array location. Otherwise, the array element is set to 0.
Operation: and (&) True (1) False (0)
True (1) True & Ture = True (1) True & False = False (0)
False (0) False & Ture = False (0) False & False = False (0)
>> a == 2 & b==3 % is true because both are true
ans =
logical
1
>> a == 2 & b==2 % is false because 2nd statement is 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 0 0 0
>> [1 0 1 0; 0 0 0 1] & [1 1 0 0; 1 0 1 1] % & can be applied to matrix, elements level operation
York University ITEC2600 By Amanda Tian
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.
Operation: or ( | ) True (1) False (0)
True (1) True | Ture = True (1) True | False = True (1)
False (0) False | Ture = True (1) False | False = False (0)
>> a == 2 | a==3 % is true because both are true
ans =
logical
York University ITEC2600 By Amanda Tian
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