attachment:OlafMatrixAlgebraScript.m of MatlabLecturesSchedule - Methods
location: attachment:OlafMatrixAlgebraScript.m of MatlabLecturesSchedule

Attachment 'OlafMatrixAlgebraScript.m'

Download

   1 %% Vector basics
   2 
   3 % Row vector
   4 x = [1 1 1 1]
   5 
   6 % Column vector
   7 y = [1 1 1 1]'
   8 
   9 % create vector with sequence of numbers
  10 z = [1:2:10]
  11 z = [-1:0.01:1]
  12 
  13 % Getting an element out of a vector
  14 z(2)
  15 z(2:3)
  16 
  17 % looping over elements
  18 for i = 1:4,
  19     display( x(i) );
  20 end;
  21 
  22 % Add or multiply values etc.
  23 x + 10
  24 2*x
  25 
  26 
  27 %% Vector multiplication
  28 % Transpose vectors
  29 x
  30 x'
  31 
  32 % Vector multiplication: row vector * column vector (for time being)
  33 x,y    % just for display
  34 x*y
  35 
  36 % Common error: multiply vectors of different lengths:
  37 z = [1 1 1]'
  38 x*z
  39 
  40 % Define two parallel vectors:
  41 x = [1 0 1 0]
  42 y = [1 0 1 0]'
  43 
  44 % Multiply vectors
  45 x*y
  46 
  47 % Define two orthogonal vectors:
  48 x = [1 0 1 0]
  49 y = [0 1 0 1]'
  50 
  51 % Multiply vectors
  52 x*y
  53 
  54 
  55 %% Some things you can do with vectors 
  56 % Define a vector
  57 x = [1 2 3 4]
  58 
  59 % Define another vector
  60 y = [0 0 1 0]'   % note the transpose
  61 
  62 % Multiply the vectors
  63 x*y
  64 
  65 % Define another vector
  66 z = [1 1 1 1]'
  67 
  68 % Sum of elements of x:
  69 x*z
  70 
  71 % Average of elements of x
  72 % length(x) yields the number of elements of a vector
  73 x*z / length(x)
  74 
  75 % Define another vector
  76 y = [1 -1 1 -1]'
  77 
  78 % Subtract sum of even from sum of odd elements in x:
  79 x*y
  80 
  81 clear
  82 %% Matrices
  83 
  84 % Define a (diagonal) matrix
  85 x = [1 1 1; 2 2 2]
  86 
  87 % Getting elements out of matrices
  88 x(1,2)
  89 x(:,1)
  90 x(2,:)
  91 
  92 % Getting matrix dimensions
  93 size(x) % returns number of rows and columns, respectively
  94 [m,n] = size(x)
  95 
  96 %% Matrices c'd
  97 % Matrix transpose
  98 x
  99 y = x'
 100 
 101 % adding/multiplying values etc.
 102 x + 2
 103 2 * x
 104 
 105 % Define another matrix
 106 z = [1 2 3; 4 5 6]
 107 
 108 % Subtract z from x
 109 x - z
 110 
 111 % Add z to x
 112 x + z
 113 
 114 % Elementwise multiplication (NOT "matrix multiplication": "*")
 115 x .* z
 116 
 117 % Try... (matrix multiplication)
 118 x * z
 119 
 120 % Elementwise division (NOT "matrix division/inversion": "/")
 121 x ./ z
 122 
 123 % Try... (matrix inversion)
 124 x / z
 125 
 126 %% Multiplying matrix and vector
 127 % Define a vector
 128 v = [1 1 1]'
 129 
 130 % Mutliply matrix x with vector v (and obtain sum within each row of x)
 131 x
 132 v
 133 x * v
 134 
 135 % matrix and vector dimensions must agree
 136 v * x % does not work
 137 
 138 %% interpretation as sum of column vectors
 139 % x * v results in the sum of column vectors in x
 140 x * v
 141 1*x(:,1) + 1*x(:,2) + 1*x(:,3)
 142 
 143 %% Matrix multiplication
 144 % Multiply two matrices
 145 x * y
 146 
 147 % Multiply two matrices
 148 y * x
 149 
 150 % Note: x*y is not necessarily y*x!!!
 151 
 152 % Define new matrix
 153 z = [1 1; 2 2]
 154 
 155 % Works:
 156 y*z
 157 z*x
 158 
 159 % Doesn't work:
 160 z*y
 161 x*z
 162 
 163 %% Make your own matrices
 164 
 165 % Identity matrix
 166 id = eye(3)
 167 
 168 % Example:
 169 x*id
 170 
 171 id*x'
 172 
 173 % diagonal matrix
 174 di = eye(3)
 175 di(1,1) = 1
 176 di(2,2) = 2
 177 di(3,3) = 3
 178 
 179 % Multiplies columns by a factor:
 180 x*di
 181 
 182 % Multiplies rows by a factor
 183 di*y
 184 
 185 %% Some useful functions
 186 
 187 % sum/average across columns of a matrix
 188 % note: most Matlab function operate column-by-column
 189 sum( x )
 190 mean( x )
 191 
 192 % sum across rows of a matrix
 193 sum( x' )
 194 mean( x' )
 195 
 196 % get the diagonal of a (square) matrix
 197 mydiag = diag(di)
 198 
 199 % dimensions of a matrix, size()
 200 [m,n] = size(x)
 201 
 202 % dimension of a vector
 203 diaglen = length(mydiag)
 204 
 205 % create a matrix/vector with ones
 206 ones(10)   % creates 10x10 matrix
 207 ones(10,1) % creates 10x1 column vector
 208 
 209 % create a matrix/vector with zeros
 210 zeros(10)   % creates 10x10 matrix
 211 zeros(1,10) % creates 1x10 row vector
 212 
 213 % create matrix/vector with standard uniform random numbers in interval 0-1
 214 rand(3)
 215 
 216 % create matrix/vector with standard normal random numbers in interval 0-1
 217 randn(3)
 218 
 219 % "reshaping" a matrix or a vector
 220 x = 1:10
 221 reshape(x,5,2)
 222 reshape(x,2,5)
 223 
 224 % Kronecker product ("concatenates" matrices/vectors)
 225 kron(id,y)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-10-14 11:15:44, 737.5 KB) [[attachment:1_CBUComputing.pdf]]
  • [get | view] (2017-06-20 13:12:05, 1993.2 KB) [[attachment:1_CBUComputing2016.pdf]]
  • [get | view] (2017-10-11 18:16:13, 1942.0 KB) [[attachment:1_CBU_Computing.pdf]]
  • [get | view] (2017-10-11 18:11:29, 3455.5 KB) [[attachment:1_CBU_Computing.ppt]]
  • [get | view] (2015-10-09 15:29:53, 319.2 KB) [[attachment:2_Linux.pdf]]
  • [get | view] (2015-10-09 15:30:29, 5009.7 KB) [[attachment:2_Linux.zip]]
  • [get | view] (2014-12-04 10:03:39, 3958.5 KB) [[attachment:3_MATLAB.zip]]
  • [get | view] (2014-12-04 10:04:18, 212.0 KB) [[attachment:3_Parallel.pdf]]
  • [get | view] (2022-10-20 09:06:47, 1581.3 KB) [[attachment:Computing2022.pdf]]
  • [get | view] (2023-11-06 13:40:01, 1841.0 KB) [[attachment:Computing2023.pdf]]
  • [get | view] (2021-10-12 09:44:32, 1427.1 KB) [[attachment:Computing_11Oct21.pdf]]
  • [get | view] (2012-11-19 16:35:37, 2434.4 KB) [[attachment:Demo.zip]]
  • [get | view] (2015-12-02 13:45:02, 26.8 KB) [[attachment:ExampleScripts.zip]]
  • [get | view] (2013-12-17 18:24:09, 11.1 KB) [[attachment:Examples and exercises]]
  • [get | view] (2012-12-04 09:06:39, 9.8 KB) [[attachment:Examples.zip]]
  • [get | view] (2012-11-27 17:48:52, 11344.7 KB) [[attachment:ExamplesOlaf.zip]]
  • [get | view] (2013-12-17 18:28:39, 11.1 KB) [[attachment:Examples_and_exercises.zip]]
  • [get | view] (2015-11-24 17:37:13, 11.9 KB) [[attachment:Examples_and_exercises_2015.zip]]
  • [get | view] (2016-11-29 15:02:26, 11.8 KB) [[attachment:Examples_and_exercises_2016.zip]]
  • [get | view] (2016-11-30 11:57:48, 12.2 KB) [[attachment:Examples_and_exercises_2016_djm.zip]]
  • [get | view] (2017-11-14 12:21:56, 3158.3 KB) [[attachment:Examples_and_exercises_2017_djm.zip]]
  • [get | view] (2019-11-05 14:51:00, 12.7 KB) [[attachment:Examples_and_exercises_2019_djm.zip]]
  • [get | view] (2019-11-05 15:23:31, 12.9 KB) [[attachment:Examples_and_exercises_2019_djm2.zip]]
  • [get | view] (2019-11-05 15:28:25, 13.0 KB) [[attachment:Examples_and_exercises_2019_djm3.zip]]
  • [get | view] (2019-11-08 09:38:24, 11.2 KB) [[attachment:Examples_and_exercises_2019_djm4.zip]]
  • [get | view] (2017-11-14 12:24:35, 12.1 KB) [[attachment:Examples_and_exercises_Nov2017_djm.zip]]
  • [get | view] (2019-11-11 10:01:12, 903.9 KB) [[attachment:Getting Started Nov2019.pdf]]
  • [get | view] (2012-11-13 13:34:09, 386.8 KB) [[attachment:Handouts.pdf]]
  • [get | view] (2014-11-04 16:08:20, 1455.9 KB) [[attachment:IntroMatrixAlgebra.zip]]
  • [get | view] (2015-11-18 17:14:47, 932.3 KB) [[attachment:IntroMatrixAlgebra_Matlab_18Nov15.pdf]]
  • [get | view] (2015-11-18 17:18:06, 1513.6 KB) [[attachment:IntroMatrixAlgebra_Matlab_18Nov15.zip]]
  • [get | view] (2013-10-30 18:24:19, 571.7 KB) [[attachment:Intro_to_CBU_computing_2013.pdf]]
  • [get | view] (2013-12-11 12:54:22, 1994.6 KB) [[attachment:JTaylorMatlabLectureDemo.zip]]
  • [get | view] (2012-11-19 16:37:32, 1024.9 KB) [[attachment:Lecture.pdf]]
  • [get | view] (2012-11-19 16:36:10, 1024.9 KB) [[attachment:Lecture.zip]]
  • [get | view] (2012-11-13 17:46:26, 380.9 KB) [[attachment:LectureHandouts.pdf]]
  • [get | view] (2012-11-27 17:47:55, 1224.2 KB) [[attachment:LectureOlaf.pdf]]
  • [get | view] (2012-11-13 17:46:46, 830.7 KB) [[attachment:LectureSlides.pdf]]
  • [get | view] (2022-10-17 14:41:11, 286.5 KB) [[attachment:Linux2022.pdf]]
  • [get | view] (2023-11-07 13:36:57, 286.5 KB) [[attachment:Linux2023.pdf]]
  • [get | view] (2017-10-27 09:25:00, 201.3 KB) [[attachment:LinuxClass_JeffBerry_19Oct17.pdf]]
  • [get | view] (2018-10-22 12:48:26, 721.5 KB) [[attachment:LinuxClass_JeffBerry_22Oct18.pdf]]
  • [get | view] (2016-10-27 08:33:27, 207.2 KB) [[attachment:LinuxClass_JeffBerry_26Oct16.pdf]]
  • [get | view] (2016-10-27 09:01:40, 4178.5 KB) [[attachment:LinuxClass_JeffBerry_26Oct16_material.tar.gz]]
  • [get | view] (2021-10-12 09:44:37, 829.5 KB) [[attachment:LinuxPart1_11Oct21.pdf]]
  • [get | view] (2021-10-12 09:44:42, 684.8 KB) [[attachment:LinuxPart2_11Oct21.pdf]]
  • [get | view] (2016-11-21 10:18:53, 488.3 KB) [[attachment:MATLAB Basics 16Nov16.pdf]]
  • [get | view] (2019-11-06 17:33:48, 16.8 KB) [[attachment:MATLAB_basic_commands_2_exercises.docx]]
  • [get | view] (2019-11-06 17:36:48, 480.3 KB) [[attachment:MATLAB_basic_commands_2_exercises.pdf]]
  • [get | view] (2014-07-31 12:11:41, 357.8 KB) [[attachment:Matlab - SPM-AA.pdf]]
  • [get | view] (2017-11-01 11:22:33, 466.9 KB) [[attachment:Matlab Basic Commands - handouts]]
  • [get | view] (2017-11-01 11:22:45, 576.0 KB) [[attachment:Matlab Basic Commands - slides]]
  • [get | view] (2018-10-23 16:10:59, 2.4 KB) [[attachment:MatlabBasicsCommands_ Delia_23Oct18.zip]]
  • [get | view] (2018-10-23 16:12:21, 1192.1 KB) [[attachment:MatlabBasicsCommands_Delia_23Oct18.pdf]]
  • [get | view] (2014-11-14 11:28:06, 269.4 KB) [[attachment:MatlabVisualisation-Handouts.pdf]]
  • [get | view] (2014-11-14 11:28:21, 314.9 KB) [[attachment:MatlabVisualisation-Slides.pdf]]
  • [get | view] (2014-11-14 11:28:41, 541.0 KB) [[attachment:MatlabVisualisation_examples.zip]]
  • [get | view] (2018-10-23 16:09:45, 695.6 KB) [[attachment:Matlab_Basic_introduction_AndreaGreve_23Oct18.pdf]]
  • [get | view] (2012-11-26 19:00:14, 11342.9 KB) [[attachment:OlafExamples.zip]]
  • [get | view] (2012-11-26 18:59:49, 1224.0 KB) [[attachment:OlafLecture.pdf]]
  • [get | view] (2013-12-12 12:02:04, 878.5 KB) [[attachment:OlafMatrixAlgebraPresentation.pdf]]
  • [get | view] (2013-12-12 12:01:39, 3.6 KB) [[attachment:OlafMatrixAlgebraScript.m]]
  • [get | view] (2012-11-13 13:35:16, 2.5 KB) [[attachment:Scripts.zip]]
  • [get | view] (2015-12-02 12:17:56, 26.8 KB) [[attachment:ScriptsVisualization]]
  • [get | view] (2012-11-13 13:35:07, 647.1 KB) [[attachment:Slides.pdf]]
  • [get | view] (2017-11-01 11:28:12, 466.9 KB) [[attachment:YaaraMatlabBasicCommandsHandouts.pdf]]
  • [get | view] (2017-11-01 11:24:21, 4.7 KB) [[attachment:YaaraMatlabBasicCommandsScripts.zip]]
  • [get | view] (2017-11-01 11:28:25, 576.0 KB) [[attachment:YaaraMatlabBasicCommandsSlides.pdf]]
  • [get | view] (2015-10-27 15:04:53, 2.6 KB) [[attachment:YaaraMatlabBasics.zip]]
  • [get | view] (2015-10-27 15:05:37, 448.8 KB) [[attachment:YaaraMatlabBasicsHandouts.pdf]]
  • [get | view] (2015-10-27 15:06:10, 564.9 KB) [[attachment:YaaraMatlabBasicsSlides.pdf]]
  • [get | view] (2019-11-06 17:35:08, 718.0 KB) [[attachment:basic_commands_2_2019.pdf]]
  • [get | view] (2019-11-06 17:33:38, 2583.2 KB) [[attachment:basic_commands_2_2019.pptx]]
  • [get | view] (2013-03-18 18:22:07, 5.0 KB) [[attachment:batch_spm5_1stlevel(1).m]]
  • [get | view] (2017-11-30 12:46:52, 6.0 KB) [[attachment:cbu_scheduler_example.m]]
  • [get | view] (2014-12-10 13:21:59, 292.6 KB) [[attachment:computing_pres2014_cluster.pdf]]
  • [get | view] (2013-12-17 18:33:34, 11.1 KB) [[attachment:examples_and_exercises]]
  • [get | view] (2013-12-17 18:34:35, 11.1 KB) [[attachment:examples_and_exercises.zip]]
  • [get | view] (2015-11-24 17:32:11, 11.9 KB) [[attachment:examples_and_exercises_2015.zip]]
  • [get | view] (2012-12-04 16:11:47, 1338.4 KB) [[attachment:lecture]]
  • [get | view] (2016-12-07 17:33:36, 1032.1 KB) [[attachment:matlabWorkshop_viz_Kate.pdf]]
  • [get | view] (2017-11-22 11:10:30, 1310.9 KB) [[attachment:matlabWorkshop_viz_Kate2017.pdf]]
  • [get | view] (2019-11-07 17:32:52, 2156.8 KB) [[attachment:matlab_lecture_Nov_2019_post.pdf]]
  • [get | view] (2019-11-08 09:40:29, 3155.9 KB) [[attachment:matlab_lecture_Nov_2019_post.pptx]]
  • [get | view] (2013-02-04 14:58:14, 3.2 KB) [[attachment:mrclogo.gif]]
  • [get | view] (2019-11-08 19:18:11, 7.5 KB) [[attachment:plottingExampleCode_ljb_20191108.m]]
  • [get | view] (2019-11-08 19:16:48, 1604.9 KB) [[attachment:visualisationInMatlab_ljb_slides_20191109.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.