<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Vectorization_and_Vector_Calculus_in_Matlab</id>
	<title>Vectorization and Vector Calculus in Matlab - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Vectorization_and_Vector_Calculus_in_Matlab"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Vectorization_and_Vector_Calculus_in_Matlab&amp;action=history"/>
	<updated>2026-06-02T11:30:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Vectorization_and_Vector_Calculus_in_Matlab&amp;diff=656&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;Questions to [mailto:davrot@uni-bremen.de David Rotermund]  &#039;&#039;By Jan Wiersig, modified by Udo Ernst and translated into English by Daniel Harnack.&#039;&#039;  A great advantage of Matlab is that all variables are matrices (or, more general, arrays).  The benefits of this principle are manifold: They encompass the simplicity of the syntax to formulate complex mathematical operations, avoidance of tedious for-loops and optimization of internal processing of huge amounts of data res...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Vectorization_and_Vector_Calculus_in_Matlab&amp;diff=656&amp;oldid=prev"/>
		<updated>2025-10-23T14:05:20Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Questions to [mailto:davrot@uni-bremen.de David Rotermund]  &amp;#039;&amp;#039;By Jan Wiersig, modified by Udo Ernst and translated into English by Daniel Harnack.&amp;#039;&amp;#039;  A great advantage of Matlab is that all variables are matrices (or, more general, arrays).  The benefits of this principle are manifold: They encompass the simplicity of the syntax to formulate complex mathematical operations, avoidance of tedious for-loops and optimization of internal processing of huge amounts of data res...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;By Jan Wiersig, modified by Udo Ernst and translated into English by Daniel Harnack.&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
A great advantage of Matlab is that all variables are matrices (or, more general, arrays).&lt;br /&gt;
&lt;br /&gt;
The benefits of this principle are manifold: They encompass the simplicity of the syntax to formulate complex mathematical operations, avoidance of tedious for-loops and optimization of internal processing of huge amounts of data resulting in considerable acceleration of the program.&lt;br /&gt;
&lt;br /&gt;
== Basic Syntax ==&lt;br /&gt;
The easiest definition of vectors and matrices is to put the elements of a matrix between square brackets. Column vectors are treated in Matlab as matrices of dimension &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\;N\times 1&amp;lt;/math&amp;gt; , row vectors as matrices of dimension &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\;1\times N&amp;lt;/math&amp;gt; and scalars (i.e. plain numbers) as matrices of dimension &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\;1\times 1&amp;lt;/math&amp;gt;.Rows are divided from other rows by the use of a semi-colon, and columns are separated by commata (oder simply by a blank space, as in the last of the following examples). Examples:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|scalar&lt;br /&gt;
|&amp;lt;code&amp;gt;a_scalar = 1;&amp;lt;/code&amp;gt;&lt;br /&gt;
|1x1-matrix&lt;br /&gt;
|-&lt;br /&gt;
|column vector&lt;br /&gt;
|&amp;lt;code&amp;gt;a_column = [1; 2; 3];&amp;lt;/code&amp;gt;&lt;br /&gt;
|3x1-matrix&lt;br /&gt;
|-&lt;br /&gt;
|row vector&lt;br /&gt;
|&amp;lt;code&amp;gt;a_row = [1, 2, 3];&amp;lt;/code&amp;gt;&lt;br /&gt;
|1x3-matrix&lt;br /&gt;
|-&lt;br /&gt;
|matrix&lt;br /&gt;
|&amp;lt;code&amp;gt;a_fullmatrix = [1 2 3; 4 5 6; 7 8 9];&amp;lt;/code&amp;gt;&lt;br /&gt;
|3x3-matrix&lt;br /&gt;
|}&lt;br /&gt;
The same rules that subsume single elements or variables to a vector also apply for strings and so-called cell-arrays:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;b = [&amp;#039;A &amp;#039; &amp;#039;character&amp;#039; &amp;#039;string&amp;#039; &amp;#039;of four elements&amp;#039;];&lt;br /&gt;
&lt;br /&gt;
c = {rand([4 4]) 1:17 complex(42) &amp;#039;this is a cell of a cell-array!&amp;#039;};&amp;lt;/syntaxhighlight&amp;gt;For the ones interested to dive deeper into Matlab: Note that cell-arrays are extremely useful to save several aspects of data in the same variable. An example from the laboratory would be a variable that holds the measured data, a date for the day of recording, a description of the experimental setup in a string and the parameters of recording. Furthermore, structures exist, whose constituent parts have names that can be accessed with a syntax similar to the programming language C.&lt;br /&gt;
&lt;br /&gt;
== Definition of Matrices and Arrays ==&lt;br /&gt;
Simple vectors can be defined with the already mentioned colon-operator. The syntax is start value:step width:end value. If the step width is omitted, the default step width of &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; is used. Examples:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;1:7&amp;lt;/code&amp;gt; creates a vector with the numbers 1, 2, 3, 4, 5, 6, 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;1:2:7&amp;lt;/code&amp;gt; creates a vector with the numbers 1, 3, 5, 7.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;4:-0.3:2.8&amp;lt;/code&amp;gt; creates a vector with the numbers 4.0, 3.7, 3.4, 3.1, 2.8.&lt;br /&gt;
&lt;br /&gt;
The vectors defined in this way shall be assigned to a variable:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;p = 0:0.01:100;&amp;lt;/syntaxhighlight&amp;gt;Such vectors, if used in loops, will be processed element-wise. In the following example, a vector with the numbers 1, 17 and 42 is printed to the screen element-wise:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;to_process = [1 17 42];&lt;br /&gt;
&lt;br /&gt;
for k=to_process;&lt;br /&gt;
&lt;br /&gt;
    fprintf(&amp;#039;k is %i\n&amp;#039;, k);&lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;The output is:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;k is 1&lt;br /&gt;
&lt;br /&gt;
k is 17&lt;br /&gt;
&lt;br /&gt;
k is 42&amp;lt;/syntaxhighlight&amp;gt;Furthermore, the colon operator can be used to access columns, rows or arbitrary dimensions of an array. The colon by &amp;#039;&amp;#039;&amp;#039;itself&amp;#039;&amp;#039;&amp;#039; selects &amp;#039;&amp;#039;&amp;#039;all&amp;#039;&amp;#039;&amp;#039; elements of the respective dimension. In the syntax introduced above, the colon operator can also be used to select a specific set of indices of a matrix:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;b = a(4, 7:5:17);&amp;lt;/code&amp;gt; selects, from the 4. row of the matrix a the entries of the columns 7, 5 and 17. It creates a vector from these elements and assigns it to the variable b.&lt;br /&gt;
&lt;br /&gt;
Bigger matrices and arrays are defined by the commands zeros, ones and eye. The two arguments of these functions assign the number of columns and rows, e.g. &amp;lt;code&amp;gt;fullofzeros = zeros(17, 42);&amp;lt;/code&amp;gt;. zeros fills the created matrix with zeros,&lt;br /&gt;
&lt;br /&gt;
ones with ones, and eye creates an identity matrix. More than two arguments result in multidimensional arrays (tensors). There are several shortened notations of these commands. Some examples:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;z = zeros([1 17]);&amp;lt;/code&amp;gt;&lt;br /&gt;
|correct creates 1x17-matrix.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;z = zeros(1, 17);&amp;lt;/code&amp;gt;&lt;br /&gt;
|correct creates 1x17-matrix.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;z = zeros([1, 17]);&amp;lt;/code&amp;gt;&lt;br /&gt;
|correct creates 1x17-matrix.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;z = zeros(1:17);&amp;lt;/code&amp;gt;&lt;br /&gt;
|wrong&lt;br /&gt;
|}&lt;br /&gt;
tries to create a 1x2x3x4x5x6x7x8x9x10x11x12x13x14x15x16x17-matrix&lt;br /&gt;
&lt;br /&gt;
and does not find enough memory!&lt;br /&gt;
&lt;br /&gt;
A similar problem can occur when invoking the commands with only one argument &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;n&amp;lt;/math&amp;gt;. Matlab will try to create a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;n&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;n&amp;lt;/math&amp;gt; -Matrix instead of a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;n&amp;lt;/math&amp;gt; -vector (which might have been intended).&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;z = zeros(1e6);&amp;lt;/code&amp;gt;&lt;br /&gt;
|wrong tries to create a 1000000x1000000-matrix!&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;z = zeros(1, 1e6);&amp;lt;/code&amp;gt;&lt;br /&gt;
|correct creates a 1x1000000-vector.&lt;br /&gt;
|}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If you are in doubt, use, as always, the help function!&amp;#039;&amp;#039;&amp;#039; Multidimensional arrays lend themselves for example to save the color planes of a digital image. A matrix for that purpose would be created by &amp;lt;code&amp;gt;a = zeros([1024 768 3]);&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the last command in this series we will cover meshgrid. meshgrid takes two vectors as arguments and duplicate the first vector in the rows of the output matrix. The second vector is duplicated along the columns of the output matrix. The number of duplications of a vector is determined by the number of elements of the other vector. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;[x_matrix, y_matrix] = meshgrid(-10:10, -10:10);&amp;lt;/code&amp;gt; creates two matrices, whose entries of the columns resp. of the rows range from &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;-10&amp;lt;/math&amp;gt; to &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;10&amp;lt;/math&amp;gt;. This command is suitable to define coordinate axes for two-dimensional plots. An example can be found in the chapter ‘Graphics in Matlab’.&lt;br /&gt;
&lt;br /&gt;
When working with initializations and arrays, it is important to be aware of how the contents of these arrays are administrated in the computer. Matlab hides many tasks form our eyes, that have to be attended to in many other programming languages first before being able to work with variables and arrays.&lt;br /&gt;
&lt;br /&gt;
One of these tasks is the declaration of variables. In a ‘proper’ programming language, one has to determine the type of a variable when introducing it. Different types are floating-point numbers, integers, strings, etc.&lt;br /&gt;
&lt;br /&gt;
If a numerical value is assigned to a variable, Matlab automatically assumes that the type of the variable is intended to be a double-precision floating-point number. Other types have to be created by an explicit type conversion.&lt;br /&gt;
&lt;br /&gt;
Another task is the allocation and deallocation of memory: An array needs memory to be represented in the computer. Before the variable can be used, the computer has to be advised first to reserve a fitting space in the memory for this variable (just as a real estate has to be bought first before one is allowed to start building a house on it). In the same manner, memory has to be freed again if it is no longer needed (to prevent that empty real estates are left in the memory that block the space from other house builders). Matlab hides these processes and does all the administrative work for us. This can become critical if one writes into a variable that was previously undefined. For example the command clear; a(100000)=pi; creates a vector with &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;100000&amp;lt;/math&amp;gt; entries, and only in the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;100000&amp;lt;/math&amp;gt;-th entry the number &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\pi&amp;lt;/math&amp;gt; is written. If the range of the vector, for which only &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;100000&amp;lt;/math&amp;gt; elements were reserved, is now exceeded by writing a(100001)=pi;, the following will happen:&lt;br /&gt;
&lt;br /&gt;
* a new vector with &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;100001&amp;lt;/math&amp;gt; elements is created.&lt;br /&gt;
* into element &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;100001&amp;lt;/math&amp;gt;, &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;2\pi&amp;lt;/math&amp;gt; is written.&lt;br /&gt;
* the elements &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; bis &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;100000&amp;lt;/math&amp;gt; are copied from the old to the new vector.&lt;br /&gt;
* the memory allocated by the old vector is deallocated.&lt;br /&gt;
* the new vector takes the name of the old vector.&lt;br /&gt;
&lt;br /&gt;
…a lot of effort to append an element. This is why you should always keep in mind to properly define and initialize arrays (with zeros or ones) before using them!&lt;br /&gt;
&lt;br /&gt;
== Simple, predefined Vector Operations ==&lt;br /&gt;
Matlab is capable to process all elements of a vector or a matrix in one go:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;a = rand([1 17]);&lt;br /&gt;
&lt;br /&gt;
b = rand([1 17]);&lt;br /&gt;
&lt;br /&gt;
c = a+b;&amp;lt;/syntaxhighlight&amp;gt;The mathematical equivalent is &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;c_i=a_i+b_i&amp;lt;/math&amp;gt;, &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\forall i=1,\ldots,17&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Vectors and matrices have to match in their dimensions;&lt;br /&gt;
&lt;br /&gt;
A prerequisite for the Addition of arrays is that all dimensions have the same size. The same holds for subtraction.&lt;br /&gt;
&lt;br /&gt;
Element-wise multiplication, division and exponentiation is indicated by a preceded dot:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;c_mult = a.*b;&lt;br /&gt;
&lt;br /&gt;
c_div = a./b;&amp;lt;/syntaxhighlight&amp;gt;This convention was introduced in Matlab to tell apart the element-wise multiplication from the matrix multiplication.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;C = [1,1;0,1];&amp;lt;/code&amp;gt;&lt;br /&gt;
|defines the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;2\times 2&amp;lt;/math&amp;gt; matrix&lt;br /&gt;
|&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;C = \left(\begin{array}{ccc}1 &amp;amp; 1\\0 &amp;amp; 1\\\end{array}\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;D = [1,2;3,1];&amp;lt;/code&amp;gt;&lt;br /&gt;
|defines the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;2\times 2&amp;lt;/math&amp;gt; matrix&lt;br /&gt;
|&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;D = \left(\begin{array}{ccc}1 &amp;amp; 2\\3 &amp;amp; 1\\\end{array}\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;G = C*D;&amp;lt;/code&amp;gt;&lt;br /&gt;
|gives&lt;br /&gt;
|&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;G = \left(\begin{array}{ccc}4 &amp;amp; 3\\3 &amp;amp; 1\\\end{array}\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
The mathematical equivalent is &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;G_{kl} = \sum_{j}C_{kj}D_{jl}&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Transposing a matrix &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;C^t_{kl} = C_{lk}&amp;lt;/math&amp;gt; is done by&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;K = C&amp;#039;;&amp;lt;/code&amp;gt;&lt;br /&gt;
|gives&lt;br /&gt;
|&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;K = \left(\begin{array}{ccc}1 &amp;amp; 0\\1 &amp;amp; 1\\\end{array}\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
The function &amp;lt;code&amp;gt;transpose(C)&amp;lt;/code&amp;gt; can be utilized alternatively.&lt;br /&gt;
&lt;br /&gt;
Inverting a matrix is accomplished by&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|&amp;lt;code&amp;gt;M = D^-1;&amp;lt;/code&amp;gt;&lt;br /&gt;
|gives&lt;br /&gt;
|&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;M = \left(\begin{array}{ccc}-0.2 &amp;amp; 0.4 \\ 0.6 &amp;amp; -0.2\\ \end{array}\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
resp. &amp;lt;code&amp;gt;M = inv(D);&amp;lt;/code&amp;gt;. Assuming a system of equations&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;D\,\vec{x} = \vec{b}&amp;lt;/math&amp;gt; and the vector &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\vec{b}&amp;lt;/math&amp;gt; with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\vec{b} = \left(\begin{array}{c}3\\ 1\\\end{array}\right) \,&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is given, the solution, by using the inverse, is found in Matlab by:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;\left(\begin{array}{c} x\\ y\\ \end{array}\right) = M \left(\begin{array}{c}3 \\ 1\\ \end{array}\right) = \left(\begin{array}{c}-0.2 \cdot 3 + 0.4 \cdot 1 \\ 0.6 \cdot 3 - 0.2 \cdot 1 \\ \end{array}\right)= \left(\begin{array}{c}-0.2 \\ 1.6 \\ \end{array}\right)&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The solution of a system of equations can also be found by using the non-inverted matrix and one of the symbols &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;/&amp;lt;/math&amp;gt;, &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;\backslash&amp;lt;/math&amp;gt; resp. the commands mldivide and mrdivide.&lt;br /&gt;
&lt;br /&gt;
Important in a physical or geometrical context is the cross product of two vectors, which is realized in Matlab by the command cross:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;a = [0; 0; 1];&lt;br /&gt;
&lt;br /&gt;
b = [1; 0; 0];&lt;br /&gt;
&lt;br /&gt;
c = cross(a, b);&lt;br /&gt;
&lt;br /&gt;
c&amp;lt;/syntaxhighlight&amp;gt;The output of these lines is:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;c = &lt;br /&gt;
&lt;br /&gt;
    0&lt;br /&gt;
&lt;br /&gt;
    1&lt;br /&gt;
&lt;br /&gt;
    0&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Reshaping, Copying and Duplication of Matrices/Arrays ==&lt;br /&gt;
A useful command to reorganize data is reshape. The content of the array is not altered by this command, only the organization in columns and rows, respectively the other dimensions. The syntax is &amp;lt;code&amp;gt;result = reshape(array, [dim1 dim2 dim3 ... dimN]);&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The result is an array of dimensions &amp;lt;code&amp;gt;dim1&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;dimN&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The product of these dimension has to equal the product of the &amp;#039;&amp;#039;&amp;#039;old&amp;#039;&amp;#039;&amp;#039; dimension of the array.&lt;br /&gt;
&lt;br /&gt;
In the following situations, the command is sensibly applied:&lt;br /&gt;
&lt;br /&gt;
* Reshaping of arrays to a form that is suitable for vector operations. Let us assume that we have &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;8&amp;lt;/math&amp;gt; vectors of &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;10&amp;lt;/math&amp;gt; constellations each in a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;3&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;8&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;10&amp;lt;/math&amp;gt; -array wit the name &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;r&amp;lt;/math&amp;gt;. Now we want to rotate the vectors with a rotation matrix &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;D&amp;lt;/math&amp;gt; ,to simulate the movement of the night sky:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;dummy = D*reshape(r, [3 80]);&lt;br /&gt;
&lt;br /&gt;
r = reshape(dummy, [3 8 10]);&amp;lt;/syntaxhighlight&amp;gt;We first reshaped the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;8&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;10&amp;lt;/math&amp;gt; -matrix of vectors to form a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;800&amp;lt;/math&amp;gt; -element vector, then we applied the rotation and finally brought the result matrix back to its original form.&lt;br /&gt;
&lt;br /&gt;
* Interpretation of data, that was initially written from a file into a one-dimensional array. Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;f = fopen(&amp;#039;digitalfphoto.bin&amp;#039;, &amp;#039;r&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
a = fread(f, &amp;#039;uint8=&amp;gt;uint8&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
fclose(f);&lt;br /&gt;
&lt;br /&gt;
imagesc(reshape(a, [500 375 3]));&lt;br /&gt;
&lt;br /&gt;
set(gca, &amp;#039;DataAspectRatio&amp;#039;, [1 1 1]);&amp;lt;/syntaxhighlight&amp;gt;The second to last command reshaped the array to enable the usage of imagesc to display the image correctly.&lt;br /&gt;
&lt;br /&gt;
In the last example, the Matlab user might be annoyed that the imagesc-command projects the first dimension of the array (with length 500, thus e.g. the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;x&amp;lt;/math&amp;gt; -axis of a photo in landscape format!) onto the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;y&amp;lt;/math&amp;gt; -axis. The versatile command permute remedies this situation – this command &amp;#039;&amp;#039;&amp;#039;swaps&amp;#039;&amp;#039;&amp;#039; single dimensions of an array. It is important to understand that not only the organization of an array in rows and columns is changed, but also the sequence of the entries in the memory! The syntax of the command is &amp;lt;code&amp;gt;permuted = permute(array, [dimidx_dim1, dimidx_dim2, ..., dimidx_dimN]);&amp;lt;/code&amp;gt;. Here, the second argument holds the numbers from &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; to &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; (where &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; is the number of dimensions of an array, i.e. &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N=2&amp;lt;/math&amp;gt; for a matrix), that specify the permutation of the dimensions. In the array permuted, the first dimension will be the dimidx_dim1-th dimension of the old array array, the second dimension the dimidx_dim2-th dimension of the old array, etc.&lt;br /&gt;
&lt;br /&gt;
In order to display the digital photo from our example correctly, we thus have to type:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;a_permuted = permute(reshape(a, [500 375 3]), [2 1 3]);&lt;br /&gt;
&lt;br /&gt;
imagesc(a_permuted);&amp;lt;/syntaxhighlight&amp;gt;This interchanges the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;x&amp;lt;/math&amp;gt; - and the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;y&amp;lt;/math&amp;gt; - direction, while the color information is preserved in the third dimension.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;repmat&amp;lt;/code&amp;gt;, another important command, duplicates an array or matrix along one or several dimensions. The syntax is similar to reshape,&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;result = repmat(array, [rep_dim1 rep_dim2 rep_dim3 ... rep_dimN]);&amp;lt;/syntaxhighlight&amp;gt;The matrix array is cloned&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;rep_dim1&amp;lt;/code&amp;gt;-times along the first dimension, rep_dim2-times along the second dimension, etc.&lt;br /&gt;
&lt;br /&gt;
For example, the following commands,&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;a = [1 2; 3 4];&lt;br /&gt;
&lt;br /&gt;
repmat(a, [3 4])&amp;lt;/syntaxhighlight&amp;gt;give the output:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;ans =&lt;br /&gt;
&lt;br /&gt;
    1      2      1      2      1      2      1      2&lt;br /&gt;
&lt;br /&gt;
    3      4      3      4      3      4      3      4&lt;br /&gt;
&lt;br /&gt;
    1      2      1      2      1      2      1      2&lt;br /&gt;
&lt;br /&gt;
    3      4      3      4      3      4      3      4&lt;br /&gt;
&lt;br /&gt;
    1      2      1      2      1      2      1      2&lt;br /&gt;
&lt;br /&gt;
    3      4      3      4      3      4      3      4&amp;lt;/syntaxhighlight&amp;gt;Using this command makes sense, if matrix operations are repeated with different operands on the same initial vector. An example may be found in the section ‘vectorization’.&lt;br /&gt;
&lt;br /&gt;
== What Size does an Array have? ==&lt;br /&gt;
Especially when manipulating a lot of different arrays, when a function is supposed to operate on an array of previously unknown size or when one wants to find out whether two arrays are compatible to serve as an input to a matrix operation, the following commands will be helpful: size, length and numel.&lt;br /&gt;
&lt;br /&gt;
It follows a short explanation for their function:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt; returns the size of an array in a vector that has as many elements as the array has dimensions. Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;size(zeros([17 42]))&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
    17     42&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;length&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
returns the longest dimension of an array, irrespective of which dimension this is. Example:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;length(zeros([17 42]))&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
    42&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;length(zeros([42 17]))&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
    42&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;numel&amp;lt;/code&amp;gt; returns the total number of all elements of an array and is identical with the construct &amp;lt;code&amp;gt;prod(size(array));&amp;lt;/code&amp;gt;. Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;numel(zeros([17 42]))&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
    714&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example: Vectorization of a Program ==&lt;br /&gt;
To close this chapter, we will discuss two examples of what to do when vectorizing a program. First, consider the problem of generating a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; -Matrix with entries &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;a_{ij} = i \cdot j&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The tedious way via loops looks like this:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;N = 17;&lt;br /&gt;
&lt;br /&gt;
a1 = zeros([N N]);&lt;br /&gt;
&lt;br /&gt;
for i=1:N, &lt;br /&gt;
    for j=1:N; &lt;br /&gt;
        a1(i, j) = i*j; &lt;br /&gt;
    end; &lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;It is much simpler to first create a row vector of the numbers &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; bis &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt;, and subsequently duplicate this this vector via repmat to obtain a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; -matrix. Finally, this matrix is multiplied element-wise with its transpose:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;N = 17;&lt;br /&gt;
&lt;br /&gt;
onefold = 1:N;&lt;br /&gt;
&lt;br /&gt;
manifold = repmat(onefold, [N 1]);&lt;br /&gt;
&lt;br /&gt;
a2 = manifold.*manifold&amp;#039;;&amp;lt;/syntaxhighlight&amp;gt;However, this is still not yet the pinnacle of elegance that can be reached. Thinking a little about this problem, one might come to the conclusion that the mathematical operation that is intended can be described as a matrix multiplication of a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; -matrix with a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;1&amp;lt;/math&amp;gt; x &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;N&amp;lt;/math&amp;gt; -matrix (the so-called outer product of two vectors):&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;N = 17;&lt;br /&gt;
&lt;br /&gt;
onefold = 1:N;&lt;br /&gt;
&lt;br /&gt;
a3 = onefold&amp;#039;*onefold;&amp;lt;/syntaxhighlight&amp;gt;As a second example we consider the following program: It defines two vectors with &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;x&amp;lt;/math&amp;gt; - and &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;y&amp;lt;/math&amp;gt; -coordinates, performs a translation of these coordinates and finally a rotation.&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;%%% Example: before vectorizing&lt;br /&gt;
&lt;br /&gt;
x = [ 0 -1  0 -2  0 -3  0 -4  0 -5  0  0  ...&lt;br /&gt;
    0  5  0  4  0  3  0  2  0  1  0];&lt;br /&gt;
y = [ 6  4  4  2  2  0  0 -2 -2 -4 -4 -6 ...&lt;br /&gt;
    -4 -4 -2 -2  0  0  2  2  4  4  6];&lt;br /&gt;
&lt;br /&gt;
%%% Translation in x- and y-direction&lt;br /&gt;
trans_x = 4;&lt;br /&gt;
trans_y = -4;&lt;br /&gt;
&lt;br /&gt;
%%% Rotation&lt;br /&gt;
phi = pi/4;&lt;br /&gt;
&lt;br /&gt;
n = numel(x);&lt;br /&gt;
for i=1:n&lt;br /&gt;
&lt;br /&gt;
    %%% apply translation to x,y&lt;br /&gt;
    x(i) = x(i)+trans_x;&lt;br /&gt;
    y(i) = y(i)+trans_y;&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
for i=1:n&lt;br /&gt;
&lt;br /&gt;
    %%% apply rotation to x,y&lt;br /&gt;
    x_temp =  x(i)*cos(phi)+y(i)*sin(phi);&lt;br /&gt;
    y_temp = -x(i)*sin(phi)+y(i)*cos(phi);&lt;br /&gt;
&lt;br /&gt;
    x(i) = x_temp;&lt;br /&gt;
    y(i) = y_temp;&lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;As a first step, we can subsume the &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;23&amp;lt;/math&amp;gt; coordinate-tuple to a &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;2x23&amp;lt;/math&amp;gt; -matrix, that we call r:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;r = [x; y];&amp;lt;/syntaxhighlight&amp;gt;Here we used the semicolon to stack the row vectors on top of each other. Every column of the matrix r now holds a coordinate tuple. In the next step, we rewrite the translation vector as well as a column vector:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;trans = [trans_x; trans_y];&amp;lt;/syntaxhighlight&amp;gt;Now we might be tempted to replace the first for-loop by an element-wise addition of trans and r. But beware! The dimensions of both variables are not compatible, trans has only one column. Thus we use the command repmat, to duplicate trans &amp;lt;math display=&amp;quot;inline&amp;quot;&amp;gt;23&amp;lt;/math&amp;gt; times. After this we can perform the addition:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;r = r+repmat(trans, [1 size(trans, 2)]);&amp;lt;/syntaxhighlight&amp;gt;Here, we used the command size to avoid having to use the constant 23 – the code will thus also work with other, longer or shorter variables x or y.&lt;br /&gt;
&lt;br /&gt;
Next up is the rotation. The rotation matrix &amp;lt;code&amp;gt;D&amp;lt;/code&amp;gt; is quickly defined,&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;D = [cos(phi) sin(phi); -sin(phi) cos(phi)];&amp;lt;/syntaxhighlight&amp;gt;and equally quickly applied to &amp;lt;code&amp;gt;r&amp;lt;/code&amp;gt;:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;r = D*r;&amp;lt;/syntaxhighlight&amp;gt;In the end, we assign the result again to the variables x and y . For this we use the colon-operator that selects whole rows of the matrix r:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;x = r(1, :);&lt;br /&gt;
&lt;br /&gt;
y = r(2, :);&amp;lt;/syntaxhighlight&amp;gt;We can even integrate rotation and translation:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;r = D*(r+repmat(trans, [1 size(r, 2)]));&amp;lt;/syntaxhighlight&amp;gt;This gives the vectorized program code:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;%%% Example: AFTER vectorization&lt;br /&gt;
&lt;br /&gt;
x = [ 0 -1  0 -2  0 -3  0 -4  0 -5  0  0  ...&lt;br /&gt;
    0  5  0  4  0  3  0  2  0  1  0];&lt;br /&gt;
y = [ 6  4  4  2  2  0  0 -2 -2 -4 -4 -6 ...&lt;br /&gt;
    -4 -4 -2 -2  0  0  2  2  4  4  6];&lt;br /&gt;
&lt;br /&gt;
%%% Translation in x- and y-direction&lt;br /&gt;
trans_x = 4;&lt;br /&gt;
trans_y = -4;&lt;br /&gt;
&lt;br /&gt;
%%% Rotation&lt;br /&gt;
phi = pi/4;&lt;br /&gt;
&lt;br /&gt;
%%% Definition of the vectors&lt;br /&gt;
r = [x; y];&lt;br /&gt;
trans = [trans_x; trans_y];&lt;br /&gt;
D = [cos(phi) sin(phi); -sin(phi) cos(phi)];&lt;br /&gt;
&lt;br /&gt;
%%% Two to three loops vectorized:&lt;br /&gt;
r = D*(r+repmat(trans, [1 size(r, 2)]));&lt;br /&gt;
&lt;br /&gt;
%%% assign the result&lt;br /&gt;
x = r(1, :);&lt;br /&gt;
y = r(2, :);&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>