MATLAB Programming/Vector and Matrices

From Wikibooks, open books for an open world
Jump to navigation Jump to search

What is scalar,vector and matrix ?[edit | edit source]

Scalar[edit | edit source]

A scalar consists of a single number, such as 3, -5, 0.368, etc.

Vector[edit | edit source]

A vector is a one dimensional array of numbers (can be in a row or column).

This example below is row vector

This example below is column vector

Matrix[edit | edit source]


A matrix is an ordered rectangular array arrangement of numbers.
A matrix having m rows and n columns is called a matrix of order m × n or simply m × n matrix.
It can be represented by one or more rows (m) AND one or more columns (n).

There are many types of matrices with followings as shown

Rectangular Matrix (Where no. of row and column are unequal) Square Matrix (Where no. of row and column are same) Row Matrix (Matrix with one row , aka row vector) Column Matrix (Matrix with one column , aka column vector) Diagonal Matrix (Square Matrix with non-diagonal elements equal to zero )

Scalar in MATLAB[edit | edit source]

Scalar in MATLAB looks like assigning a variable to a number such as followed:

 a = 6

Vectors in MATLAB[edit | edit source]

You can type following in MATLAB

For row vector , just type comma "," to separate each number

 >> VR = [6,2,5]
 VR =
    6     2     5

For column vector , just type semicolon ";" to separate each number

 >> VC = [9;1;6]
 VC =

Matrix in MATLAB[edit | edit source]

In MATLAB, to create matrix (or matrices), there is 3 important operator to be used

(a) Bracket "[" "]" as container for the matrix
(b) Comma , as matrix row separator
(c) Semicolon ; as matrix column separator

For example, we are creating 4X3 matrix in MATLAB using following commands.

>> M = [4,8,9,6;9,6,9,6;3,6,9,6]

M =
     4     8     9     6
     9     6     9     6
     3     6     9     6