So I defined:
v =Array{Vector{Int64}, 5}
Now I want v[1] to be the empty vector.
I tried:
v[1] = Int64[] ,
v[1]=Vector{Int64}()
It does not work.
How can I access this vector and fill it with a value?
This v = Array{Vector{Int64}, 5} is a type not an instance. You can use some of these options:
julia> v = Vector{Vector{Int}}(undef,5);
julia> v[1] = Int[];
julia> v[2] = [1,2,3];
julia> v
5-element Vector{Vector{Int64}}:
[]
[1, 2, 3]
#undef
#undef
#undef
Or if you don't mind starting with empty vectors, this can be simpler:
julia> v = [Int[] for i=1:5];
julia> v[2] = [1,2,3];
julia> v
5-element Vector{Vector{Int64}}:
[]
[1, 2, 3]
[]
[]
[]
You want v =Array{Vector{Int64}, 5}(undef, s1, s2, s3, s4, s5) (where the ss are the size of each dimension) otherwise, v is the type, not an object of that type.
Related
Example
let a = [a, b, c]
let b = [x, y, z]
I need this response
let c = [[a, b, c],[x, y, z]]
There are two approaches as I see it, either I concatenate both arrays (don't know if it's possible), or to append it directly in a multidimensional array.
If you will try to concatenate it:
let a = [1, 2, 3]
let b = [4, 5]
let c = a + b // will receive [1, 2, 3, 4, 5]
let d = [a, b] // will receive multidimensional array [[1, 2, 3], [4, 5]]
var e: [[Int]] = []
e.append(a)
e.append(b)
print(e) // should be equal to d
I am wondering if there is a 1 liner to do this assignment in the array in Julia:
h = .1
L = 1
x = 0:h:L
n = length(x)
discretized = zeros(n,n)
#really any old function
f(x,y) = x*y + cos(x) + sin(y)
for i in 1:n
for j in 1:n
discretized[i, j] = f(x[i], x[j])
end
end
Or do I explicitly have to write out the loops?
You could broadcast the function over an array an its transpose - julia will return the result as a 2d Array:
x = 0:0.1:1
f(x,y) = x*y + cos(x) + sin(y)
A = f.(x,x') # the `.` before the bracket broadcasts the dimensions
# 11×11 Array{Float64,2}
or if have more complicated expressions or functions and don't want to write out lots of dots use the #. macro, e.g:
A = #. f(x,x') + x^2
Once A already exists, you can also do
#. A = f(x,x') + x^2
which uses .= to write the result locally to each element of A, and hence is non-allocating.
Broadcasting goes much further than this easy extension of scalar functions to arrays, allowing "fusion" of multiple calculations into a single fast operation https://julialang.org/blog/2017/01/moredots
You could do:
discretized = [f(i, j) for i in x, j in x]
For more information, see https://docs.julialang.org/en/v1/manual/arrays/#Comprehensions-1
Edit: Based on the comments, here's a brief overview of what the : operator does in indexing:
julia> a = [1, 2, 3]
3-element Array{Int64,1}:
1
2
3
julia> a[:]
3-element Array{Int64,1}:
1
2
3
julia> ans === a
false
julia> a[:] .= [2, 3, 4]
3-element view(::Array{Int64,1}, :) with eltype Int64:
2
3
4
julia> a
3-element Array{Int64,1}:
2
3
4
How do I zip a two-dimensional array with a "vector" row-wise in Julia?
This
X = [1 2; 3 4]
ndims(X)
Y = [-1 -2]
ndims(Y)
first(zip(X,Y))
gives (1, -1) while I want to get ([1 2], -1).
If you're ok with using column-vectors for the input and output, then you can use the eachrow function, which iterates over the rows of a matrix and returns the rows as column-vectors:
julia> X = [1 2; 3 4];
julia> Y = [-1, -2];
julia> collect(zip(eachrow(X), Y))
2-element Array{Tuple{Array{Int64,1},Int64},1}:
([1, 2], -1)
([3, 4], -2)
On the other hand, if you need the first elements of your zipped tuples to be row-vectors (as is shown in your question), then you could convert your matrix into a vector of rows and then use zip:
julia> X = [1 2; 3 4];
julia> Y = [-1 -2];
julia> rows = [X[[i], :] for i in 1:size(X, 1)]
2-element Array{Array{Int64,2},1}:
[1 2]
[3 4]
julia> collect(zip(rows, Y))
2-element Array{Tuple{Array{Int64,2},Int64},1}:
([1 2], -1)
([3 4], -2)
Note that I've used X[[i], :] inside the comprehension instead of X[i, :], so that we get an array of rows rather than an array of column-vectors.
Finally, just to be clear, note that Y = [-1 -2] produces a row-vector. We usually represent vectors as column vectors:
julia> Y = [-1, -2]
2-element Array{Int64,1}:
-1
-2
There are iterator builders in Julia: eachrow and eachcol, which work for arrays and are concise (at least in this case):
X = [1 2; 3 4]
Y = [-1 -2]
z = zip(eachrow(X), eachcol(Y))
Then
for el in z
print(el)
end
gives
([1, 2], [-1])
([3, 4], [-2])
I have a numpy array
X = [[1,2], [3,4], [5,6], [1,2], [5,6]]
I want a numpy array Y = [1, 2, 3, 1, 3], where [1,2] is replaced by 1, [3,4] replaced by 2 and so on. This is for a very large (think millions) X.
Intuition is Y[X == [1,2]] = 1. But this does't work.
Intuition is Y[X == [1,2]] = 1. But this does't work.
Here is how to make it work:
Y = np.empty(len(X), dtype=np.int)
Y[np.all(X == [1, 2], 1)] = 1
To process all the possible values:
s = set(map(tuple, X))
r = np.arange(1, len(s) + 1) # or assign whatever values you want
cond = [np.all(X == v, 1) for v in s]
Y = np.dot(r, cond)
I have a Vector x=[1.00, 1.50, 1.00, 2.30, 4.20, 1.00] and also another one n=[2, 1, 3].
I would like to transform my Vector x into a Vector of Vectors as follows:
x[1] = [1.00, 1.50]
x[2] = 1.00
x[3] = [2.30, 4.20, 1.00]
where in each Vector of x, the dimension is determined by n.
What could be the faster way to implement this? Thanks!
Though not very sure about the speed, we can also use comprehension (as usual):
julia> x = [1.00, 1.50, 1.00, 2.30, 4.20, 1.00]
julia> n = [2, 1, 3]
julia> off = [ 0 ; cumsum(n) ] # offset indices
4-element Array{Int64,1}:
0
2
3
6
julia> Vector{Float64}[ x[ off[i]+1 : off[i]+n[i] ] for i=1:length(n) ]
3-element Array{Array{Float64,1},1}:
[1.0,1.5]
[1.0]
[2.3,4.2,1.0]
In future versions (>= v0.5?), we may need copy() for each vector to make the obtained vector independent of the original x[:] (though not very sure...)
julia> Vector{Float64}[ copy( x[ off[i]+1 : off[i]+n[i] ] ) for i=1:length(n) ]
Another approach:
x = [1.00, 1.50, 1.00, 2.30, 4.20, 1.00]
n = [2, 1, 3]
g = Array{Any}(length(n))
for i in 1:length(n)
o = 1 + sum(n[1:(i-1)])
p = sum(n[1:i])
g[i] = x[o:p]
end
g
Output:
3-element Array{Any,1}:
[1.0,1.5]
[1.0]
[2.3,4.2,1.0]