Wednesday, 23 March 2016

Julia: Multi Dimensional arrays


Julia provides number of ways to create an array. One simple way is use space-separated values for the columns and semicolon separated for the rows.
julia> matrix=[1 2 3; 4 5 6; 7 8 9]
3x3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

Access elements in multi dimensional array

matrix[1, 2] is used to access element at row 1 and column 2.
julia> matrix
3x3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9

julia> matrix[1, 1]
1

julia> matrix[1, 2]
2

julia> matrix[1, 3]
3

julia> matrix[2, 1]
4

julia> matrix[2, 2]
5

julia> matrix[2, 3]
6


Create a random array
matrix1=rand(3, 4) creates a two dimensional array with 3 rows and 5 columns and place random values between 0 and 1.

matrix1=rand(3, 4, 5) creates three dimensional array.
julia> matrix1=rand(3, 4)
3x4 Array{Float64,2}:
 0.872026  0.634166  0.51894   0.857864
 0.912579  0.878201  0.842193  0.763185
 0.734912  0.657344  0.751813  0.838469

julia> matrix1=rand(3, 4, 5)
3x4x5 Array{Float64,3}:
[:, :, 1] =
 0.44696   0.992225  0.816472  0.259122
 0.563711  0.910862  0.678817  0.920116
 0.093492  0.382077  0.224922  0.110865

[:, :, 2] =
 0.280802  0.820251  0.654117  0.669563
 0.421619  0.445241  0.418007  0.896429
 0.481211  0.908848  0.660048  0.35104 

[:, :, 3] =
 0.825946  0.102693  0.70998   0.449289
 0.223327  0.9735    0.609226  0.160192
 0.361002  0.430907  0.140391  0.808443

[:, :, 4] =
 0.621156  0.613405  0.814428  0.546086 
 0.753033  0.255638  0.191037  0.0907537
 0.247239  0.522408  0.361561  0.332052 

[:, :, 5] =
 0.527386  0.96858   0.467722  0.272733
 0.680959  0.641348  0.635359  0.396472
 0.475553  0.516872  0.44976   0.648296


Array(type, dims...) : Create an uninitialized array
julia> arr1 = Array(Int64, 3, 4)
3x4 Array{Int64,2}:
 13221328696  13221628992  13221665648  13221665768
 13221403288  13221665608  13221665688  13221665808
 13221391776  13221519424  13221665728            0


cell(dims...) : Create an uninitialized cell array
julia> arr2=cell(3, 4)
3x4 Array{Any,2}:
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef


zeros(type, dims...) : Create an array of all zeros of specified type
If you don’t mention type, by default it takes Float64.
julia> arr3=zeros(3, 4)
3x4 Array{Float64,2}:
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0

julia> typeof(arr3)
Array{Float64,2}

julia> 

julia> arr4=zeros(Bool, 3, 4)
3x4 Array{Bool,2}:
 false  false  false  false
 false  false  false  false
 false  false  false  false

julia> typeof(arr4)
Array{Bool,2}


zeros(A): Create an array of all zeros of type array A
julia> arr3=[1 2; 3 4]
2x2 Array{Int64,2}:
 1  2
 3  4

julia> arr4=zeros(arr3)
2x2 Array{Int64,2}:
 0  0
 0  0


ones(type, dims...) : Create an array of all ones of specified type
If you don’t mention type, by default it takes Float64.
julia> arr1=ones(2, 3)
2x3 Array{Float64,2}:
 1.0  1.0  1.0
 1.0  1.0  1.0

julia> arr1=ones(Bool, 2, 3)
2x3 Array{Bool,2}:
 true  true  true
 true  true  true

julia> arr1=ones(Int32, 2, 3)
2x3 Array{Int32,2}:
 1  1  1
 1  1  1


ones(A): an array of all ones of same element type and shape of A
julia> arr1=[1 23.4; 23.4 1]
2x2 Array{Float64,2}:
  1.0  23.4
 23.4   1.0

julia> arr2=ones(arr1)
2x2 Array{Float64,2}:
 1.0  1.0
 1.0  1.0


trues(dims...): Define Boolean array with all true values
julia> arr1=trues(2, 3)
2x3 BitArray{2}:
 true  true  true
 true  true  true


falses(dims...): Define Boolean array with all false values
julia> arr1=falses(2, 3)
2x3 BitArray{2}:
 false  false  false
 false  false  false


reshape(A, dims...): Copy all the elements of array A with different dimensions
julia> arr1=[2 3; 4 5]
2x2 Array{Int64,2}:
 2  3
 4  5

julia> arr2=reshape(arr1, 1, 4)
1x4 Array{Int64,2}:
 2  4  3  5

julia> arr2=reshape(arr1, 4, 1)
4x1 Array{Int64,2}:
 2
 4
 3
 5

julia> arr2=reshape(arr1, 2, 2)
2x2 Array{Int64,2}:
 2  3
 4  5


copy(A): Create a shallow copy of array A
julia> arr1=[2 3; 4 5]
2x2 Array{Int64,2}:
 2  3
 4  5

julia> arr2=copy(arr1)
2x2 Array{Int64,2}:
 2  3
 4  5


deepcopy(A): Create deep copy of array A
julia> arr1=[2 3; 4 5]
2x2 Array{Int64,2}:
 2  3
 4  5

julia> arr2=deepcopy(arr1)
2x2 Array{Int64,2}:
 2  3
 4  5


similar(A, element_type, dims...)
Creates an uninitialized array of same type as A. Second and third arguments are optional, if you don’t specify second and third arguments, it take the type and dimensions of A.
julia> arr=["Hi" "How"; "are" "you"]
2x2 Array{ASCIIString,2}:
 "Hi"   "How"
 "are"  "you"

julia> arr1=similar(arr)
2x2 Array{ASCIIString,2}:
 #undef  #undef
 #undef  #undef

julia> arr1=similar(arr, ASCIIString, 1, 4)
1x4 Array{ASCIIString,2}:
 #undef  #undef  #undef  #undef

julia> arr1=similar(arr, ASCIIString, 2, 4)
2x4 Array{ASCIIString,2}:
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef

julia> arr1=similar(arr, Int64, 2, 4)
2x4 Array{Int64,2}:
 4681313248  4633103696  4668749072  4681313296
 4681313264  4668749040  4614627408  4614627408

julia> arr1=similar(arr, Float32, 2, 4)
2x4 Array{Float32,2}:
 1.03687e-26  1.54228e-25  1.78763e-27  0.0
 1.4013e-45   1.4013e-45   1.4013e-45   0.0


reinterpret(type, A)
Creates an array with the same binary data as the given array, but with the specified element type

rand(dims)
Create an array with given dimensions, place random values between 0 and 1.
julia> arr=rand(2, 3)
2x3 Array{Float64,2}:
 0.0422532  0.916317  0.12345  
 0.748497   0.601301  0.0567454

julia> arr=rand(3, 4)
3x4 Array{Float64,2}:
 0.260864  0.415536   0.751686  0.244284
 0.802792  0.970866   0.484614  0.610748
 0.225755  0.0629364  0.12472   0.219201


randn(dims)
Create an array of Float64s with random, standard normally distributed random values.
julia> arr=randn(3, 4)
3x4 Array{Float64,2}:
  0.156047   0.31567   1.04309  -0.0894308
 -0.664208  -1.57193  -1.45034   0.69307  
 -1.85717   -1.29193  -0.21012   0.570295 

julia> arr=randn(2, 4)
2x4 Array{Float64,2}:
 -0.982935  0.17478   -0.258679   1.0398  
 -0.147887  0.758829   1.27381   -0.667129


eye(n): Create nXn identity matrix
julia> eye(2)
2x2 Array{Float64,2}:
 1.0  0.0
 0.0  1.0

julia> eye(3)
3x3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0


eye(m, n): Create mXn identity matrix
julia> eye(2, 3)
2x3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0

julia> eye(5, 4)
5x4 Array{Float64,2}:
 1.0  0.0  0.0  0.0
 0.0  1.0  0.0  0.0
 0.0  0.0  1.0  0.0
 0.0  0.0  0.0  1.0
 0.0  0.0  0.0  0.0


fill!(A, x): Fill the array A with the value x
julia> a=Array(Int32, 3, 3)
3x3 Array{Int32,2}:
           2  0          0
           2  0          0
 -2147483647  0  319538512

julia> arr1=fill!(a, 23)
3x3 Array{Int32,2}:
 23  23  23
 23  23  23
 23  23  23


fill(x, dims): Create an array with given dimensions and fill with value x

julia> arr=fill("ptr", 3, 3)
3x3 Array{ASCIIString,2}:
 "ptr"  "ptr"  "ptr"
 "ptr"  "ptr"  "ptr"
 "ptr"  "ptr"  "ptr"











Previous                                                 Next                                                 Home

No comments:

Post a Comment