Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Going Beyond One Dimension (Matrices)

Authors
Affiliations
University of São Paulo
University of São Paulo

So far we have worked with structures that have more than one dimension, but without looking closely at their type. In this lesson we will try to understand the differences between them and how this can be used to our advantage.

Let’s start with lists:

v = [1, 2, 3]
typeof(v)
Vector{Int64} (alias for Array{Int64, 1})

The type returned is: Vector{Int64} (alias for Array{Int64, 1}). In this case this means that v is a vector of integers, or a one-dimensional array. Likewise

v = zeros(Int64, 3)
typeof(v)
Vector{Int64} (alias for Array{Int64, 1})

But vectors can be more flexible, as in the example below:

v = [1, 2.0, "três"]
typeof(v)
Vector{Any} (alias for Array{Any, 1})

In this case the vector’s type is no longer integers and becomes “Any”, that is, Vector{Any} (alias for Array{Any, 1}).

Furthermore, imagine the following situation:

a = [1, 2, 3]
push!(v, a)
typeof(v)
Vector{Any} (alias for Array{Any, 1})

In this case, the vector remains of type Any, but in the fourth position we now have a vector with three integers. This shows us that vector structures can be quite flexible. However, this flexibility with different types usually comes at some cost, generally in performance.

On the other hand, structures can also have more than one dimension. These are called matrices, and they can be created with the zeros function that we already used above.

m = zeros(Int64, 3, 2)
typeof(m)
Matrix{Int64} (alias for Array{Int64, 2})

Above we created a two-dimensional matrix with three rows and two columns. Its elements can be accessed just like in a vector, but now with two indices.

m[1, 2]  = 10
10
function imprime(m::Array{Int64,2})
    println(m)
end
imprime (generic function with 1 method)
function imprime(m::Vector{Vector{Int64}})
    println(m[1])
    println(m[2])
end
imprime (generic function with 2 methods)
function imprime(m::Vector{Vector{Int64}})
    for i in m
        println(i)
    end
 end
imprime (generic function with 2 methods)
function imprime(m::Vector{Vector{Int64}})
    for i in m
        for j in m[i]
            println(j,"  ")
        end   
    end
end
imprime (generic function with 2 methods)
function imprime(m::Vector{Vector{Int64}})
    for i in m
        print("|")
        for j in i
            print(j,"  ")
        end
        println("|")   
    end
end
imprime (generic function with 2 methods)
function imprimeMatriz(m::Matrix{Int64})
    println(m)
end
imprimeMatriz (generic function with 1 method)
function imprimeMatriz(m::Matrix{Int64})
    i = 1
    while i < size(m)[1]
        println(m[1])
        i += 1
    end
end
imprimeMatriz (generic function with 1 method)
function imprimeMatriz(m::Matrix{Int64})
    i = 1
    while i < size(m)[1]
        j = 1
        while j < size(m)[2]
            print(m[i, j], " ")
            j += 1
        end
        println()   
        i += 1
    end
end
imprimeMatriz (generic function with 1 method)
function preencheMatriz(m::Matrix{Int64})
    i = 1
    while i <= length(m)
        m[i] = rand(Int) % 10
        i += 1
    end
end
preencheMatriz (generic function with 1 method)
function criaIdentidate(tam::Int64)
    m = zeros(Int64, tam, tam)
    i = 1
    while i <= tam
        m[i, i] = 1
    end
    return m  
end
criaIdentidate (generic function with 1 method)

Direct operations on matrices such as +, - and *