# Array Operations

**Get started handling multidimensional arrays in Julia with this simple guide.**

Now that we know several ways of inputting arrays, we should take a look at how we can operate with them.

## Getting the Size of an Array

The size of an array can be obtained with the ‘size’ function. Importantly, the elements of `s=size(A)`

are treated as an array of integers, one for each dimension, even in the one-dimensional case. That is, we should always employ a syntax of the form `s[dim]`

.

```
A = rand(6)
s = size(A)
print(s) # returns (6,)
print(s[1]) # returns 6
```

## Matrix multiplication

Two dimensional arrays (or matrices) are a fundamental part of Julia. For example, you can resort to a Matlab-style syntax for matrix-matrix multiplication:

```
A * B
```

A Matrix and a Vector can be also multiplied with the * operator.

```
A * v
```

## Element-wise multiplication

In case you need to multiply the elements of an n-dimensional array on an element-wise fashion, you can resort to the dot operator, which will *broadcast* the scalar multiplication operator on an element-wise fashion, just as we discussed in the previous post of this tutorial series:

```
A .* B
```

## Dot product

You can do dot products by calling the `dot`

function

```
v = rand(1000)
w = rand(1000)
z = dot(v,w)
```

Alternatively, you can resort to a typical linear algebra notation:

```
z = v'w
```

## Array slicing

Array slicing is a very useful operation in Linear Algebra, which consists in extracting a subset of elements from an array and packaging them as another array. In Julia, we can resort to a very straightforward syntax for this operation.

For example, the code below will extract the odd indices in both dimensions from a 6x6 matrix, and store the results in a new 3x3 matrix.

```
A = rand(6,6) # 6×6 Matrix{Float64}
B = A[1:2:5,1:2:5] # 3×3 Matrix{Float64}
```

## Logical Indexing

A very useful way of selecting array elements is to resort to a boolean array, in a process which is often referred to as logical indexing. This can be easily done in Julia by resorting to broadcasting the logical operations of interest.

For example, let’s generate a matrix of random numbers and set to zero all numbers in the array which are below 0.5:

```
A = rand(6,6)
A[ A .< 0.5 ] .= 0
```

Note that in this example we also needed to broadcast the assignment operator as well.

## Backslash operator

Just like in Matlab, Julia has a built-in operator to solve matrices. For square matrices, it will try to solve the linear system, while for rectangular matrices, it will seek for the least squares solution.

One **‘gotcha’** that you will probably encounter sooner or later is that, as **1xN matrices are not the same as N-element vectors**. We have to be careful and always employ vectors in the right-hand side of an equation.

```
b1 = [4.0, 5, 6] # 3-element Vector{Float64}
b2 = [4.0; 5; 6] # 3-element Vector{Float64}
m1 = [4.0 5 6] # 1×3 Matrix{Float64}
x=A\b1 # Solves A*x=b
x=A\b2 # Solves A*x=b
x=A\m1 # Error!!
```