# Multiplying matrices element by element on GNU Octave

Hey scientist!
Today we’ll continue our series about matrices on GNU Octave! In this post we’ll talk about multiplying matrices and vectors. Let’s do this!

Do you remember how to define vectors and matrices, dear scientist? If not, check the previous post on the series. There you’ll learn also how to calculate the transpose of these elements.

There are two ways to multiply vectors and matrices: element by element, known as Hadamard product, and the “real” multiplication. For instance, check the following matrices matA and matB:

```>> matA = [1 0 3; 2 1 5; 3 6 0];
>> matB = [2 4 1; 6 0 2; 4 4 0];
```

Octave does not show the obtained matrices when we use a semicolon at the end of the command. If you want to see the object, it’s sufficient to type its name and type :

```>> matA
matA =
1   0   3
2   1   5
3   6   0

>> matB
matB =
2   4   1
6   0   2
4   4   0
```

These matrices contains three rows and three columns. To check the size of an element, we can use the size() function:

```>> size(matA)
ans =
3   3

>> size(matB)
ans =
3   3
```

In the first case, multiplication element by element, each element of the first matrix is multiplied with its corresponding on the second matrix:

•  Element on the first row, first column on the first matrix times the element on the first row, first column on the second matrix: 1 * 2 = 2.
• Element on the first row, second column on the first matrix times the element on the first row, second column on the second matrix: 0 * 4 = 0.

You got the point. Each element on the first matrix is multiplied with the element of the second matrix, on the same position.

To multiply element by element in Octave we use the operator dot and times (.*). In our example, the operation is done as follows:

```>> matA .* matB
ans =
2    0    3
12    0   10
12   24    0
```

The only limitation is that the vectors/matrices must have the same size. If not, some elements won’t have a multiplying pair. Check matC on the next example, and what happens when we try to multiply it by matA or matB:

```>> matC = [1 4; 0 2]
matC =
1   4
0   2

>> matA .* matC
error: product: nonconformant arguments (op1 is 3x3, op2 is 2x2)

>> matB .* matC
error: product: nonconformant arguments (op1 is 3x3, op2 is 2x2)
```

A last tip: use the operators add (+) and subtract () to add and subtract matrices. The rule is the same: the vectors/matrices must have the same size! Try it on the previous examples!

That’s it scientist! Next week we’ll see more multiplication of vectors and matrices.
Gigaregards! See you next time!

Did you like this post? Please comment and share with your friends!