ENG1060 Lecture Notes - Lecture 2: Matlab, Concatenation

54 views5 pages
1D##(aka#vectors)
="an"object/variables"that"contains"multiple"values
Contains"rows"and"columns
Scalar
Row vector"(eg."[1"21"36"44"25]">>>"aka"1-by-5"matrix
A = [1,2,3,4] or [1 2 3 4]
Column vector"(aka"5-by-1"matrix)
Actual"matrix
Row">>>" column"or"vice"versa:"use"transpose"operator"apostrophe"'"(or"
transpose function)
(eg."C = A' /"D = transpose(B)]
Colon operator"(i.e."matrix"with"pattern)
[start_value : step : end_value]
A = 6 :1 : 10
If A = 6:10, automatically"step"is"taken"to"be"1
A = 7:-3:0 >>>"stops"at"1,"doesn't"go"down"to"-2"until"-2"is"
breached
For"column,"round"brackets"w/"apostrophe
§
To"create"a"vector"with"equally-spaced"values"using"a"specified"number"
of"points (eg."7"equally"spaced"numbers"bwtn"3.851"and"7.84),"where"
you"would"normally"have"to"calculate"the"difference"manually
linspace(start_value, end_value, num_of_points)
If"num_of_points"not"specified,"default"100
If"num_of_points"is"negative,"error
2D#(aka#faces/surfaces)
="contains"multiple"rows"and"columns
[1 2 3; 4 5 6] or"[1:3;4:6] >>>"creates"2x3
Matrix"with"same"element"throughout:
zeros(rows,columns)
Put"number"for"same"element"in"front,"spell"out
1s"in"diagonal"line"towards"right"throughout,"rest"zero
eye(rows,columns)
Matrix"full"of"random"numbers"frm"0-1
rand(rows,columns)
Properties
Length"of"1D"matrix"/"length"of"horiz/vert"
length(X)
Will"only"give"longer"length
Both"dimensions"of"2D"matrix
[r,c] = size(X)
Concatenation
MUST"have"at"least"one"dimension"same
A = [1:3;4:6]
X = [A B]
()"for"1D"///"address"matrices
[]"for"2D"///"create"matrices
,"or"<space>"is"another"element"in"
same"row
;"creates"another"row
:"specifies"range
%plotting
plot(Tf,rho,'r-','linewidth',2)
xlabel('Temperature in Celcius
(\circC)')
ylabel('Density in g/cm^3')
title('Freshwater density')
%% part b
%finding the maximum and
corresponding address
[t_max, idx] = max(rho)
%plotting maximum point
Tf_max = Tf(idx)
hold on
plot(Tf(idx),rho(idx),'k^')
Matrix#calculation
Inner"dimensions"of"matrix"must"match"for"multiplication"to"work
(eg."1x3"*"3x3"="1x3;"1x4"*"3x1"NOT"OK)
Element-by-elementops
="calculations"between"corresponding elems"of"matrices
Place"a"dot"prior"to"the"op
Must"be"done"btwn"matrices"of"same"size
Can"perform:
Add,"subtraction"(no"need"dot"prior)
Multiplication"[.*],"division"[./],"exponentiation"[.^]
DIFFERENT"frm"normal"ops"because"inner"
dimensions"need"not"be"the"same
§
Scalar#multiplication/addition#etc.
MATLAB"will"create"a"matrix"of"same"size"as"matrix,"all"elements"are"
the"scalar"number
No"need"dot"in"front"of"multiply/div/expo
Built-in#math#ops
sin, cos, tan, sqrt, round, log, etc.
All"operate"on"each"element"individually
For"matrices:
Sum, mean, median, min, max, sort, unique
Unique"returns"only"one"of"each"value"in"matrix,"
useful"if"matrix"has"elements"that"are"repeated
§
maximum_in_A = max(A)
§
[maximum_in_A,index_of_max] = max(A)
§
Matrix#addressing/indexing
Can"refer"to"numbers"within"a"vector/matrix"to"
perform"calcs
Creates"smaller"matrices"frm"a"large"matrix
A(index)
Index"is"scalar,"can"be"1,…,end
Runs"vertically,"so"A(2)"is"below"A(1)
To"reference"multiple"values"frm"a"vector
A([1 4 5 6]) -sequence"of"indexes"to"be"
extracted
Better"than"manually"extracting"values"using"
index
Accessing"by"rows"and"columns
A(row_index, column_index)
Vectors"can"be"used"as"indices
Colon by"itself">>>"return"all"rows/columns
To"extract"certain"rows"and"certain"columns:
C"="a(1:3"(row indexes),"[2"3"5]"(column indexes))
Week$2:$Matrices
Sunday,"30"July"2017
21:34
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in
1D##(aka#vectors)
="an"object/variables"that"contains"multiple"values
Contains"rows"and"columns
Scalar
Row vector"(eg."[1"21"36"44"25]">>>"aka"1-by-5"matrix
A = [1,2,3,4] or [1 2 3 4]
Column vector"(aka"5-by-1"matrix)
B = [5;6;7;8]
Actual"matrix
Row">>>" column"or"vice"versa:"use"transpose"operator"apostrophe"'"(or"
transpose function)
(eg."C = A' /"D = transpose(B)]
Colon operator"(i.e."matrix"with"pattern)
[start_value : step : end_value]
A = 6 :1 : 10
If A = 6:10, automatically"step"is"taken"to"be"1
A = 7:-3:0 >>>"stops"at"1,"doesn't"go"down"to"-2"until"-2"is"
breached
For"column,"round"brackets"w/"apostrophe
A = (0:5:20)'
§
To"create"a"vector"with"equally-spaced"values"using"a"specified"number"
of"points (eg."7"equally"spaced"numbers"bwtn"3.851"and"7.84),"where"
you"would"normally"have"to"calculate"the"difference"manually
linspace(start_value, end_value, num_of_points)
If"num_of_points"not"specified,"default"100
If"num_of_points"is"negative,"error
2D#(aka#faces/surfaces)
="contains"multiple"rows"and"columns
[1 2 3; 4 5 6] or"[1:3;4:6] >>>"creates"2x3
Matrix"with"same"element"throughout:
zeros(rows,columns)
Put"number"for"same"element"in"front,"spell"out
1s"in"diagonal"line"towards"right"throughout,"rest"zero
eye(rows,columns)
Matrix"full"of"random"numbers"frm"0-1
rand(rows,columns)
Properties
Length"of"1D"matrix"/"length"of"horiz/vert"
length(X)
Will"only"give"longer"length
Both"dimensions"of"2D"matrix
[r,c] = size(X)
Concatenation
MUST"have"at"least"one"dimension"same
X = [A B]
()"for"1D"///"address"matrices
[]"for"2D"///"create"matrices
,"or"<space>"is"another"element"in"
same"row
;"creates"another"row
:"specifies"range
%plotting
plot(Tf,rho,'r-','linewidth',2)
xlabel('Temperature in Celcius
(\circC)')
ylabel('Density in g/cm^3')
title('Freshwater density')
%% part b
%finding the maximum and
corresponding address
[t_max, idx] = max(rho)
%plotting maximum point
Tf_max = Tf(idx)
hold on
plot(Tf(idx),rho(idx),'k^')
Matrix#calculation
Inner"dimensions"of"matrix"must"match"for"multiplication"to"work
(eg."1x3"*"3x3"="1x3;"1x4"*"3x1"NOT"OK)
Element-by-elementops
="calculations"between"corresponding elems"of"matrices
Place"a"dot"prior"to"the"op
Must"be"done"btwn"matrices"of"same"size
Can"perform:
Add,"subtraction"(no"need"dot"prior)
Multiplication"[.*],"division"[./],"exponentiation"[.^]
DIFFERENT"frm"normal"ops"because"inner"
dimensions"need"not"be"the"same
§
Scalar#multiplication/addition#etc.
MATLAB"will"create"a"matrix"of"same"size"as"matrix,"all"elements"are"
the"scalar"number
No"need"dot"in"front"of"multiply/div/expo
Built-in#math#ops
sin, cos, tan, sqrt, round, log, etc.
All"operate"on"each"element"individually
For"matrices:
Sum, mean, median, min, max, sort, unique
Unique"returns"only"one"of"each"value"in"matrix,"
useful"if"matrix"has"elements"that"are"repeated
§
maximum_in_A = max(A)
§
[maximum_in_A,index_of_max] = max(A)
§
Matrix#addressing/indexing
Can"refer"to"numbers"within"a"vector/matrix"to"
perform"calcs
Creates"smaller"matrices"frm"a"large"matrix
A(index)
Index"is"scalar,"can"be"1,…,end
Runs"vertically,"so"A(2)"is"below"A(1)
To"reference"multiple"values"frm"a"vector
A([1 4 5 6]) -sequence"of"indexes"to"be"
extracted
Better"than"manually"extracting"values"using"
index
Accessing"by"rows"and"columns
A(row_index, column_index)
Vectors"can"be"used"as"indices
Colon by"itself">>>"return"all"rows/columns
To"extract"certain"rows"and"certain"columns:
C"="a(1:3"(row indexes),"[2"3"5]"(column indexes))
Week$2:$Matrices
Sunday,"30"July"2017 21:34
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 5 pages and 3 million more documents.

Already have an account? Log in

Document Summary

, or is another element in same row. Row vector (eg. [1 21 36 44 25] >>> aka 1-by-5 matrix. Row >>> column or vice versa: use transpose operator apostrophe " (or transpose function) (eg. c = a" / d = transpose(b)] If a = 6:10, automatically step is taken to be 1. A = 7:-3:0 >>> stops at 1, doesn"t go down to -2 until -2 is breached. To create a vector with equally-spaced values using a specified number of points (eg. 7 equally spaced numbers bwtn 3. 851 and 7. 84), where you would normally have to calculate the difference manually linspace(start_value, end_value, num_of_points) [1 2 3; 4 5 6] or [1:3;4:6] >>> creates 2x3. Put number for same element in front, spell out. 1s in diagonal line towards right throughout, rest zero eye(rows,columns) Matrix full of random numbers frm 0-1 rand(rows,columns) Length of 1d matrix / length of horiz/vert length(x)

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents