attachment:Example Scripts 26Feb13.m of EssentialsofMRIAnalysis 2013 - MRC CBU Imaging Wiki
location: attachment:Example Scripts 26Feb13.m of EssentialsofMRIAnalysis 2013

Attachment 'Example Scripts 26Feb13.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: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 % Add or multiply values etc.
  18 x + 10
  19 2*x
  20 
  21 
  22 %% Vector multiplication
  23 % Transpose vectors
  24 x
  25 x'
  26 
  27 % Vector multiplication: row vector * column vector (for time being)
  28 x,y    % just for display
  29 x*y
  30 
  31 % Common error: multiply vectors of different lengths:
  32 z = [1 1 1]'
  33 x*z
  34 
  35 % Define two parallel vectors:
  36 x = [1 0 1 0]
  37 y = [1 0 1 0]'
  38 
  39 % Multiply vectors
  40 x*y
  41 
  42 % Define two orthogonal vectors:
  43 x = [1 0 1 0]
  44 y = [0 1 0 1]'
  45 
  46 % Multiply vectors
  47 x*y
  48 
  49 
  50 %% Some things you can do with vectors 
  51 % Define a vector
  52 x = [1 2 3 4]
  53 
  54 % Define another vector
  55 y = [0 0 1 0]'   % note the transpose
  56 
  57 % Multiply the vectors
  58 x*y
  59 
  60 % Define another vector
  61 z = [1 1 1 1]'
  62 
  63 % Sum of elements of x:
  64 x*z
  65 
  66 % Average of elements of x
  67 x*z / length(x)
  68 
  69 % Define another vector
  70 y = [1 -1 1 -1]'
  71 
  72 % Subtract sum of even from sum of odd elements in x:
  73 x*y
  74 
  75 clear
  76 %% Matrices
  77 
  78 % Define a (diagonal) matrix
  79 x = [1 1 1; 2 2 2]
  80 
  81 % Getting elements out of matrices
  82 x(1,2)
  83 x(:,1)
  84 x(2,:)
  85 
  86 % Matrix transpose
  87 x
  88 y = x'
  89 
  90 % adding/multiplying values etc.
  91 x + 2
  92 2 * x
  93 
  94 % Define another matrix
  95 z = [1 2 3; 4 5 6]
  96 
  97 % Subtract z from x
  98 x - z
  99 
 100 % Add z to x
 101 x + z
 102 
 103 % Elementwise multiplication (NOT "matrix multiplication": "*")
 104 x .* z
 105 
 106 % Try... (matrix multiplication)
 107 x * z
 108 
 109 % Elementwise division (NOT "matrix division/inversion": "/")
 110 x ./ z
 111 
 112 % Try... (matrix inversion)
 113 x / z
 114 
 115 %% Multiplying matrix and vector
 116 % Define a vector
 117 v = [1 1 1]'
 118 
 119 % Mutliply matrix x with vector v (and obtain sum within each row of x)
 120 x
 121 v
 122 x * v
 123 
 124 % matrix and vector dimensions must agree
 125 v * x % does not work
 126 
 127 %% interpretation as sum of column vectors
 128 % x * v results in the sum of column vectors in x
 129 x * v
 130 1*x(:,1) + 1*x(:,2) + 1*x(:,3)
 131 
 132 %% Matrix multiplication
 133 % Multiply two matrices
 134 x * y
 135 
 136 % Multiply two matrices
 137 y * x
 138 
 139 % Note: x*y is not necessarily y*x!!!
 140 
 141 % Define new matrix
 142 z = [1 1; 2 2]
 143 
 144 % Works:
 145 y*z
 146 z*x
 147 
 148 % Doesn't work:
 149 z*y
 150 x*z
 151 
 152 %% Make your own matrices
 153 
 154 % Identity matrix
 155 id = eye(3)
 156 
 157 % Example:
 158 x*id
 159 
 160 id*x'
 161 
 162 % diagonal matrix
 163 di = eye(3)
 164 di(1,1) = 1
 165 di(2,2) = 2
 166 di(3,3) = 3
 167 
 168 % Multiplies columns by a factor:
 169 x*di
 170 
 171 % Multiplies rows by a factor
 172 di*y
 173 
 174 %% Some useful functions
 175 
 176 % get the diagonal of a (square) matrix
 177 diag(di)
 178 
 179 % create a matrix/vector with ones
 180 ones(10)   % creates 10x10 matrix
 181 ones(10,1) % creates 10x1 column vector
 182 
 183 % create a matrix/vector with zeros
 184 zeros(10)   % creates 10x10 matrix
 185 zeros(1,10) % creates 1x10 row vector
 186 
 187 % create matrix/vector with standard uniform random numbers in interval 0-1
 188 rand(3)
 189 
 190 % create matrix/vector with standard normal random numbers in interval 0-1
 191 randn(3)
 192 
 193 % "reshaping" a matrix or a vector
 194 x = 1:10
 195 reshape(x,5,2)
 196 reshape(x,2,5)
 197 
 198 % Kronecker product ("concatenates" matrices/vectors)
 199 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] (2013-03-15 17:25:09, 262.2 KB) [[attachment:EMA_syllabus_060313.pdf]]
  • [get | view] (2013-03-19 10:19:59, 3.0 KB) [[attachment:Example Scripts 26Feb13.m]]
  • [get | view] (2013-03-19 10:18:51, 1.3 KB) [[attachment:Example Scripts GLM 27Feb13.m]]
  • [get | view] (2013-03-19 10:15:39, 642.6 KB) [[attachment:IntroGLM_27Feb13.pdf]]
  • [get | view] (2013-03-19 10:23:10, 823.8 KB) [[attachment:IntroMatrixAlgebra]]
  • [get | view] (2013-03-19 10:15:50, 823.8 KB) [[attachment:IntroMatrixAlgebra_Matlab_26Feb13.pdf]]
  • [get | view] (2013-03-15 17:35:59, 1567.6 KB) [[attachment:MEGforward_Stenroos_EMA13.pdf]]
  • [get | view] (2013-03-15 17:36:07, 1514.0 KB) [[attachment:MRPhysics.ppt]]
  • [get | view] (2013-03-15 17:36:13, 4907.5 KB) [[attachment:MRPhysicsII.ppt]]
  • [get | view] (2013-03-19 10:21:14, 3.0 KB) [[attachment:example_algebra]]
 All files | Selected Files: delete move to page copy to page

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