Category Archives: Matlab

Operations in Matlab

The following arithmetic operations are defined in MATLAB:

1) addition +
2) subtraction –
3) multiplication *
4) division /
5) elevation to power

When the expression to be evaluated is too long to fit on a single command line, it is possible to use a continuation character given by. . . (three points). Eg:

>> 1 + 1/4 + …
1/8

ans =

1.3750

It is possible to verify that since in MATLAB there is no distinction between whole, real or complex variables, the result of the operation of elevation to power in the case of fractional exponents may not correspond to that naturally expected. For example, if you want to calculate the cubic root of -5, you get:

>> (-9) ^ (1/5)

ans =

1.2555 + 0.9122i

then an imaginary number instead of a real number. The problem is due to the fact that MATLAB, working in complex notation, calculates as the first cubic root of -9 one of the two with non-imaginary imaginary part.

Operations in Matlab

Also in MATLAB it is possible to alter the classical precedences of the arithmetic operations through the appropriate use of the round brackets. For example, if in the previous case we had written (- 9) * 1/5 we would have obtained the value -1.8000 that is, correctly, – 9/5.

>> (- 9) * 1/5

ans =

-1.8000

As far as the vectors are concerned, elementary operations extend (when well defined) in a completely natural way, with the exception of division and power-up operations. Eg,

>> a = [1: 4];
>> b = [1: 3];
>> c = [3 2 6 -1];

>> a + c (sum of row carriers)

ans =

4 4 9 3

>> a-c (line array difference)

ans =

-2 0 -3 5

>> a + b
??? Error using ==> plus
Matrix dimensions must agree.

>> a * c
??? Error using ==> mtimes
Inner matrix dimensions must agree.

The last instructions show us that the operations between vectors are valid only if the dimensions are consistent. In fact, given a line vector s of dimension n (ie a matrix 1 * n) and a vector column d of size m, we can execute the scalar product s * d only if m = n (obtaining in this case a scalar).

>> s = [1: 4];

>> d = [1; 2; 3; 4];

>> s * d

ans =
30

So d * s will provide a m * n matrix.

>> s = [1: 4];

>> d = [1; 2; 3; 4];

>> M = d * s

M =

1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

Colon operator in Matlab

Colon operator (:) is one of the most important MATLAB operators. It is found in many different forms. For example, the following expression:

1:10

represents a row vector that contains integers from 1 to 10

1 2 3 4 5 6 7 8 9 10

To obtain a specific sequence instead, an increase must be indicated. For example:

>> 100: -7: 50

ans =

100 93 86 79 72 65 58 51

that is, a sequence of numbers from 100 to 50 with a step equal to -7; while

>> 0: pi / 4: pi

ans =

0 0.7854 1.5708 2.3562 3.1416

Colon operator in Matlab

The subscribed expressions involving the operator two points assign portions of a matrix. For example, we assign the usual matrix:

A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]

and let’s extract some values:

A (1: k, j)

represents the first k elements of the jth column of A. Then we extract some elements:

>> A (1: 4.4)

ans =

4
8
12
16

and we calculate the sum of the fourth column.

>> sum (A (1: 4.4))

ans =

40

The colon assigns all the elements in a row or column of a matrix and the keyword END assigns the last row or column. So

>> sum (A (:, end))

ans =

40

calculates the sum of the elements in the last column of A.

Access to elements of an array in Matlab

In this post, we see how to access the elements of an array in the Matlab environment; first of all, then insert a sample matrix in the Matlab workspace:

>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]

A =

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

The element in line i and column j of A is denoted with the symbol A (i, j). Eg:

>> A (4.2)

ans =

14

is the number in the fourth row and second column. For our matrix it corresponds to 14. Thus it is possible to calculate the sum of the elements in the fourth column of A by typing:

A (1,4) + A (2,4) + A (3,4) + A (4,4)

This produces:

>> A (1,4) + A (2,4) + A (3,4) + A (4,4)

ans =

40

but it is not the most elegant way to add a single column. It is also possible to access the elements of a matrix using a single subscript, A (k). This is the usual way to access the elements of row vectors and column vectors.

Access to elements of an array in Matlab

But it can also be applied to a two-dimensional array in this case the matrix is ​​considered as a column vector, formed by the columns of the original matrix. Thus, for our matrix, A (8) is another way of identifying the value 14 stored in position A (4.2), ie fourth row second column.

>> A (8)

ans =

14

If you try to access the elements of an array outside it, you make a mistake:

>> t = A (4,5)
??? Attempted to access A (4.5); index out of bounds because size (A) = [4.4].

(Error message)

On the other hand, if we store a new value in an element of the matrix that exceeds its order, there is an increase in order to accommodate the newcomer:

>> A (4.5) = 17

A =

1 2 3 4 0
5 6 7 8 0
9 10 11 12 0
13 14 15 16 17

The carriers are treated in Matlab in a completely similar way. You can view the individual entries of a vector, after having defined it as follows:

V = [2 1 3 4];

to view the first item, simply type:

>> V (1)
ans =
2

This command prints the first element in the vector. Also note that a new variable called ans was created.

How to enter matrices in Matlab

You can enter MATLAB matrices in many different ways.

  • introduce an explicit list of elements.
  • load arrays from external data files.
  • generate arrays using the built-in function.
  • create matrices with their own functions in M-files.

Let’s start by showing how to register the matrix as a list of its elements. In this regard, only some basic conventions are followed:

  • Separate the elements of a row with blanks or commas.
  • Use a semicolon “;” to indicate the end of each row.
  • Enclose the entire list of items with square brackets, [].

How to enter matrices in Matlab

To enter the Durer matrix, simply type:

A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

MATLAB exposes only the typed matrix to video,

A = 16 3 2 13
    5 10 11 8
    9  6 7 12
    4 15 14 1

This procedure precisely associates numbers with portions of memory. Once the matrix is supplied, it is automatically registered in the MATLAB workspace. Now you can indicate it simply by typing the letter A. at the Matlab prompt.

>> A

A =

     16 3 2 13
      5 10 11 8
      9 6 7 12
      4 15 14 1