Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
MATLAB
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Vectors and matrices === A simple array is defined using the colon syntax: ''initial''<code>:</code>''increment''<code>:</code>''terminator''. For instance: <syntaxhighlight lang="matlabsession"> >> array = 1:2:9 array = 1 3 5 7 9 </syntaxhighlight> defines a variable named <code>array</code> (or assigns a new value to an existing variable with the name <code>array</code>) which is an array consisting of the values 1, 3, 5, 7, and 9. That is, the array starts at 1 (the ''initial'' value), increments with each step from the previous value by 2 (the ''increment'' value), and stops once it reaches (or is about to exceed) 9 (the ''terminator'' value). The ''increment'' value can actually be left out of this syntax (along with one of the colons), to use a default value of 1. <syntaxhighlight lang="matlabsession"> >> ari = 1:5 ari = 1 2 3 4 5 </syntaxhighlight> assigns to the variable named <code>ari</code> an array with the values 1, 2, 3, 4, and 5, since the default value of 1 is used as the increment. [[One-based indexing|Indexing]] is one-based,<ref>{{cite web|title=Matrix Indexing|url=http://www.mathworks.com/help/matlab/math/matrix-indexing.html|publisher=MathWorks|access-date=August 14, 2013}}</ref> which is the usual convention for [[matrix (mathematics)|matrices]] in mathematics, unlike zero-based indexing commonly used in other programming languages such as C, [[C++]], and [[Java (programming language)|Java]]. Matrices can be defined by separating the elements of a row with blank space or comma and using a semicolon to separate the rows. The list of elements should be surrounded by square brackets <code>[]</code>. Parentheses <code>()</code> are used to access elements and subarrays (they are also used to denote a function argument list). <syntaxhighlight lang="matlabsession"> >> A = [16, 3, 2, 13 ; 5, 10, 11, 8 ; 9, 6, 7, 12 ; 4, 15, 14, 1] A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 >> A(2,3) ans = 11 </syntaxhighlight> Sets of indices can be specified by expressions such as <code>2:4</code>, which evaluates to <code>[2, 3, 4]</code>. For example, a submatrix taken from rows 2 through 4 and columns 3 through 4 can be written as: <syntaxhighlight lang="matlabsession"> >> A(2:4,3:4) ans = 11 8 7 12 14 1 </syntaxhighlight> A square [[identity matrix]] of size ''n'' can be generated using the function <code>eye</code>, and matrices of any size with zeros or ones can be generated with the functions <code>zeros</code> and <code>ones</code>, respectively. <syntaxhighlight lang="matlabsession"> >> eye(3,3) ans = 1 0 0 0 1 0 0 0 1 >> zeros(2,3) ans = 0 0 0 0 0 0 >> ones(2,3) ans = 1 1 1 1 1 1 </syntaxhighlight> [[Transpose|Transposing]] a vector or a matrix is done either by the function <code>transpose</code> or by adding dot-prime after the matrix (without the dot, prime will perform [[conjugate transpose]] for complex arrays): <syntaxhighlight lang="matlabsession"> >> A = [1 ; 2], B = A.', C = transpose(A) A = 1 2 B = 1 2 C = 1 2 >> D = [0, 3 ; 1, 5], D.' D = 0 3 1 5 ans = 0 1 3 5 </syntaxhighlight> Most functions accept arrays as input and operate element-wise on each element. For example, <code>mod(2*J,n)</code> will multiply every element in ''J'' by 2, and then reduce each element modulo ''n''. MATLAB does include standard <code>for</code> and <code>while</code> loops, but (as in other similar applications such as [[APL (programming language)|APL]] and [[R (programming language)|R]]), using the [[Array programming|vectorized]] notation is encouraged and is often faster to execute. The following code, excerpted from the function ''magic.m'', creates a [[magic square]] ''M'' for odd values of ''n'' (MATLAB function <code>meshgrid</code> is used here to generate square matrices {{mvar|I}} and {{mvar|J}} containing {{tmath|1:n}}): <syntaxhighlight lang="matlab"> [J,I] = meshgrid(1:n); A = mod(I + J - (n + 3) / 2, n); B = mod(I + 2 * J - 2, n); M = n * A + B + 1; </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
MATLAB
(section)
Add topic