= [1, 2, 3]
v typeof(v)
Vector{Int64} (alias for Array{Int64, 1})
Até o momento trabalhamos com estruturas com mais de uma dimensão, mas sem olharmos muito bem o seu tipo. Nessa aula vamos procurar entender as diferenças entre elas e como isso pode ser usado ao nosso favor.
Vamos começar com as listas:
= [1, 2, 3]
v typeof(v)
Vector{Int64} (alias for Array{Int64, 1})
O tipo devolvido é: Vector{Int64} (alias for Array{Int64, 1}). No caso isso significa que v é um vetor de inteiros, ou um array de uma dimensão. Da mesma forma
= zeros(Int64, 3)
v typeof(v)
Vector{Int64} (alias for Array{Int64, 1})
Mas, vetores podem ser mais flexíveis, como por exemplo abaixo:
= [1, 2.0, "três"]
v typeof(v)
Vector{Any} (alias for Array{Any, 1})
Nesse caso o tipo de vetor, deixa de ser de inteiros e passa a ser “Any”, ou seja Vector{Any} (alias for Array{Any, 1}).
Mais ainda, imaginem a seguinte situação:
= [1, 2, 3]
a push!(v, a)
typeof(v)
Vector{Any} (alias for Array{Any, 1})
Nesse caso, o vetor continua sendo do tipo Any, mas na quarta posição temos um vetor com três inteiros. Com isso podemos ver que as estruturas de vetores podem ser bem flexíveis. Mas, apesar disso, quando temos estruturas de tipos diferentes, com muita flexibilidade, geralmente há alguma penalidade de uso, geralmente no desempenho.
Por outro lado, podemos ter estruturas com mais de uma dimensão, no caso elas são denominadas matrizes. Elas podem ser criadas com a função zeros que já usamos acima.
= zeros(Int64, 3, 2)
m typeof(m)
Matrix{Int64} (alias for Array{Int64, 2})
Acima foi criada uma matriz de duas dimensões com 3 linhas e duas colunas. Seus elementos podem se acessados como em um vetor, mas agora com dois indíces.
1, 2] = 10 m[
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})
= 1
i while i < size(m)[1]
println(m[1])
+= 1
i end
end
imprimeMatriz (generic function with 1 method)
function imprimeMatriz(m::Matrix{Int64})
= 1
i while i < size(m)[1]
= 1
j while j < size(m)[2]
print(m[i, j], " ")
+= 1
j end
println()
+= 1
i end
end
imprimeMatriz (generic function with 1 method)
function preencheMatriz(m::Matrix{Int64})
= 1
i while i <= length(m)
= rand(Int) % 10
m[i] += 1
i end
end
preencheMatriz (generic function with 1 method)
function criaIdentidate(tam::Int64)
= zeros(Int64, tam, tam)
m = 1
i while i <= tam
= 1
m[i, i] end
return m
end
criaIdentidate (generic function with 1 method)
Operações diretas com matrizes tipo +, - e *