Multiplying two multi-dimensional arrays, say, a 1-dimensional with a 3-dimensional array:
[1 2] * reshape(1:8,2,2,2)
gives me the error message:
LoadError: MethodError: `*` has no method matching *(::Array{Int64,2}, ::Array{Int64,3})
Closest candidates are:
*(::Any, ::Any, !Matched::Any, !Matched::Any...)
*{TA,TB}(::Union{DenseArray{TA,1},DenseArray{TA,2},SubArray{TA,1,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD},SubArray{TA,2,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, !Matched::Base.LinAlg.AbstractTriangular{TB,S<:AbstractArray{T,2}})
*{TA,TQ,N}(::Union{DenseArray{TA,N},SubArray{TA,N,A<:DenseArray{T,N},I<:Tuple{Vararg{Union{Colon,Int64,Range{Int64}}}},LD}}, !Matched::Union{Base.LinAlg.QRCompactWYQ{TQ,M<:AbstractArray{T,2}},Base.LinAlg.QRPackedQ{TQ,S<:AbstractArray{T,2}}})
...
while loading In[167], in expression starting on line 1
in Ac_mul_B at operators.jl:157
using the math definition of multi-dimensional matrix algebra for a (1 by 2) * (2 by 2 by 2) multiplication of matrices/arrays.
A somewhat more general example can be A*B = C meaning sum_k A_{i,j,k} B_{k,l,m} = C_{i,j,l,m}, where A is a 3-index matrix, or tensor if you like, B is a 3-index one, and the resulting C is a four index matrix/tensor, but, in general, there can be any number of dimensions and a dimension can have any size (within reason). See more info on the definition of a matrix product or tensor contraction.
What is the right syntax of this multiplication in Julia?
You can use reshape to convert the multi-dimensional arrays into matrices,
multiply them, and convert the result back to a multi-dimensional array.
A = [1 2]
B = reshape(1:8,2,2,2)
reshape( reshape(A,2,1)' * reshape(B,2,4), 2, 2 )
(In this example, since A is already a matrix, there is actually no need to reshape it.)
Matrices multiplication: use *
Matrices element-wise multiplication: use .*
If you want to multiply two matrices A*B, the size of both must much: e.g.
size of A (a1,a2),
size of B (b1, b2);
in order to make the multiplication feasible a2 must be the same as b1
In your case:
julia> A=[1 2]
1x2 Array{Int64,2}:
1 2
julia> size(A) #remember that size(A') is (2,1)
(1,2)
julia> B=reshape(1:8,2,2,2)
2x2x2 Array{Int64,3}:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
julia> size(B)
(2,2,2)
julia> size(B[:,:,1])
(2,2)
julia> A*B[:,:,1]
1x2 Array{Int64,2}:
5 11
julia> A*B[:,:,2]
1x2 Array{Int64,2}:
17 23
Edit: Multiplication in "one shot" (separated for clarity):
julia> A*B[:,:,1]
1x2 Array{Int64,2}:
5 11
julia> A*B[:,:,2]
1x2 Array{Int64,2}:
17 23
julia> C=A*B[:,:]
1x4 Array{Int64,2}:
5 11 17 23
julia> size(C)
(1,4)
julia> D1 = reshape(C, 2,2)
2x2 Array{Int64,2}:
5 17
11 23
julia> D2 = reshape(C, 2,2)'
2x2 Array{Int64,2}:
5 11
17 23
Truly in one shot:
julia> D = reshape(A*B[:,:], 2,2)
2x2 Array{Int64,2}:
5 17
11 23
Related
I can't find an answer to this simple question.
I have the following:
A(a,j)=[a*j*i*k for i in 1:2, k in 1:2];
B=[A(a,j) for a in 1:2, j in 1:2];
B is a an array of arrays: 2×2 Array{Array{Int64,2},2}. This is useful to easily access the subarrays with indices (e.g., B[2,1]). However, I also need to convert B to a 4 by 4 matrix. I tried hcat(B...) but that yields a 2 by 8 matrix, and other options are worse (e.g., cat(Test2...;dims=(2,1))).
Is there an efficient way of writing B as a matrix while keeping the ability to easily access its subarrays, especially as B gets very large?
Do you want this:
julia> hvcat(size(B,1), B...)
4×4 Array{Int64,2}:
1 2 2 4
2 4 4 8
2 4 4 8
4 8 8 16
or without defining B:
julia> hvcat(2, (A(a,j) for a in 1:2, j in 1:2)...)
4×4 Array{Int64,2}:
1 2 2 4
2 4 4 8
2 4 4 8
4 8 8 16
What about
B = reduce(hcat, reduce(vcat, A(a,j) for a in 1:2) for j in 1:2)
EDIT: Actually this is very slow, I would recommend making a function, e.g.,
function buildB(A, n)
A0 = A(1,1)
nA = size(A0, 1)
B = Array{eltype(A0),2}(undef, n * nA, n * nA)
for a in 1:n, j in 1:n
B[(a-1)*nA .+ (1:nA), (j-1)*nA .+ (1:nA)] .= A(a,j)
end
return B
end
or maybe consider a package like BlockArrays.jl?
EDIT 2 This is an example with BlockArrays.jl:
using BlockArrays
function blockarrays(A, n)
A0 = A(1,1)
nA = size(A0, 1)
B = BlockArray{eltype(A0)}(undef_blocks, fill(nA,n), fill(nA,n))
for a in 1:n, j in 1:n
setblock!(B, A(a,j), a, j)
end
return B
end
which should do what you want:
julia> B = blockarrays(A, 2)
2×2-blocked 4×4 BlockArray{Int64,2}:
1 2 │ 2 4
2 4 │ 4 8
──────┼───────
2 4 │ 4 8
4 8 │ 8 16
julia> getblock(B, 1, 2)
2×2 Array{Int64,2}:
2 4
4 8
julia> B[4,2]
8
Suppose I have the following 4x4 Array in Julia:
julia> A=rand(4,4)
4×4 Array{Float64,2}:
0.00624508 0.624399 0.458094 0.341848
0.303817 0.269487 0.580949 0.534655
0.748525 0.419411 0.469938 0.659914
0.730659 0.191461 0.996144 0.74772
And I have another 6x2 Array where each row represents a row-column pair:
julia> B=[ 1 1; 1 3; 2 2; 2 4; 3 1; 3 3]
6×2 Array{Int64,2}:
1 1
1 3
2 2
2 4
3 1
3 3
The first row of B represents the element [1,1] of A, the second row of B represents the element [1,3] of A, and so on. I want to access the elements of A based on the coordinates given by each row of B. In R for example, the command A[B] gives exactly what I want: 0.00624508 0.458094 0.269487 0.534655 0.748525 0.469938 but in Julia, the same command gives
julia> A[B]
6×2 Array{Float64,2}:
0.00624508 0.00624508
0.00624508 0.748525
0.303817 0.303817
0.303817 0.730659
0.748525 0.00624508
0.748525 0.748525
and this is not what I want. Is there a similar way of coding A[B] in Julia, so that I obtain what I obtain in R? Must be applicable to any Array.
Construct an array of CartesianIndex out of your index pairs:
julia> A[CartesianIndex.(B[:, 1], B[:, 2])]
6-element Array{Float64,1}:
0.987200021334854
0.5261639427155012
0.8430528192705655
0.5300778835366697
0.5044387593056074
0.0025132074927423087
This is necessary to distinguish such "point indexing" from "shape indexing", as you observed it (I made those terms up).
Ideally, you would already construct B as such an array, but that is not always feasible, of course.
let's say we have an array of cartesian indices in Julia
julia> typeof(indx)
Array{CartesianIndex{2},1}
Now we want to plot them as a scatter-plot using PyPlot. so we should convert the indx-Array of Cartesian to a 2D-Matrix so we can plot it like this:
PyPlot.scatter(indx[:, 1], indx[:, 2])
How can i convert an Array of type Array{CartesianIndex{2},1} to a 2D-Matrix of type Array{Int,2}
By the way here is a code snippet how to produce a dummy Array of cartesianindex:
A = rand(1:10, 5, 5)
indx = findall(a -> a .> 5, A)
typeof(indx) # this is an Array{CartesianIndex{2},1}
Thanks
An easy and generic way is
julia> as_ints(a::AbstractArray{CartesianIndex{L}}) where L = reshape(reinterpret(Int, a), (L, size(a)...))
as_ints (generic function with 1 method)
julia> as_ints(indx)
2×9 reshape(reinterpret(Int64, ::Array{CartesianIndex{2},1}), 2, 9) with eltype Int64:
1 3 4 1 2 4 1 1 4
2 2 2 3 3 3 4 5 5
This works for any dimensionality, making the first dimension the index into the CartesianIndex.
One possible way is hcat(getindex.(indx, 1), getindex.(indx,2))
julia> #btime hcat(getindex.($indx, 1), getindex.($indx,2))
167.372 ns (6 allocations: 656 bytes)
10×2 Array{Int64,2}:
4 1
3 2
4 2
1 3
4 3
5 3
2 4
5 4
1 5
4 5
However, note that you don't need to - and therefore probably shouldn't - bring your indices to 2D-Matrix form. You could simply do
PyPlot.scatter(getindex.(indx, 1), getindex.(indx, 2))
In Octave, I can do
octave:1> A = [1 2; 3 4]
A =
1 2
3 4
octave:2> A(A>1) -= 1
A =
1 1
2 3
but in Julia, the equivalent syntax does not work.
julia> A = [1 2; 3 4]
2x2 Array{Int64,2}:
1 2
3 4
julia> A[A>1] -= 1
ERROR: `isless` has no method matching isless(::Int64, ::Array{Int64,2})
in > at operators.jl:33
How do you conditionally assign values to certain array or matrix elements in Julia?
Your problem isn't with the assignment, per se, it's that A > 1 itself doesn't work. You can use the elementwise A .> 1 instead:
julia> A = [1 2; 3 4];
julia> A .> 1
2×2 BitArray{2}:
false true
true true
julia> A[A .> 1] .-= 1000;
julia> A
2×2 Array{Int64,2}:
1 -998
-997 -996
Update:
Note that in modern Julia (>= 0.7), we need to use . to say that we want to broadcast the action (here, subtracting by the scalar 1000) to match the size of the filtered target on the left. (At the time this question was originally asked, we needed the dot in A .> 1 but not in .-=.)
In Julia v1.0 you can use the replace! function instead of logical indexing, with considerable speedups:
julia> B = rand(0:20, 8, 2);
julia> #btime (A[A .> 10] .= 10) setup=(A=copy($B))
595.784 ns (11 allocations: 4.61 KiB)
julia> #btime replace!(x -> x>10 ? 10 : x, A) setup=(A=copy($B))
13.530 ns ns (0 allocations: 0 bytes)
For larger matrices, the difference hovers around 10x speedup.
The reason for the speedup is that the logical indexing solution relies on creating an intermediate array, while replace! avoids this.
A slightly terser way of writing it is
replace!(x -> min(x, 10), A)
There doesn't seem to be any speedup using min, though.
And here's another solution that is almost as fast:
A .= min.(A, 10)
and that also avoids allocations.
To make it work in Julia 1.0 one need to change = to .=. In other words:
julia> a = [1 2 3 4]
julia> a[a .> 1] .= 1
julia> a
1×4 Array{Int64,2}:
1 1 1 1
Otherwise you will get something like
ERROR: MethodError: no method matching setindex_shape_check(::Int64, ::Int64)
I have an array filled with some values. After running this code:
array = zeros(10)
for i in 1:10
array[i] = 2*i + 3
end
The array looks like:
10-element Array{Float64,1}:
5.0
7.0
9.0
11.0
13.0
15.0
17.0
19.0
21.0
23.0
I would like to obtain, for example, the following array by removing the third value:
9-element Array{Float64,1}:
5.0
7.0
11.0
13.0
15.0
17.0
19.0
21.0
23.0
How to do that?
EDIT
If I have an array (and not a vector), like here:
a = [1 2 3 4 5]
1×5 Array{Int64,2}:
1 2 3 4 5
The deleteat! proposed is not working:
a = deleteat!([1 2 3 4 5], 1)
ERROR: MethodError: no method matching deleteat!(::Array{Int64,2}, ::Int64)
You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.
Closest candidates are:
deleteat!(::Array{T,1} where T, ::Integer) at array.jl:875
deleteat!(::Array{T,1} where T, ::Any) at array.jl:913
deleteat!(::BitArray{1}, ::Integer) at bitarray.jl:961
I don't want a column vector. I would want:
1×4 Array{Int64,2}:
2 3 4 5
Is it possible ?
To make that clear: Vector{T} in Julia is just a synonym for Array{T, 1}, unless you're talking about something else... we call Arrays of all ranks arrays.
But this seems to be a Matlab-inherited misconception. In Julia, you construct a Matrix{T}, ie., an Array{T, 2}, by using spaces in the literal:
julia> a = [1 2 3 4 5]
1×5 Array{Int64,2}:
1 2 3 4 5
Deleting from a matrix does not make sense in general, since you can't trivially "fix the shape" in a rectangular layout.
A Vector or Array{T, 1} can be written using commas:
julia> a = [1, 2, 3, 4, 5]
5-element Array{Int64,1}:
1
2
3
4
5
And on this, deleteat! works:
julia> deleteat!(a, 1)
4-element Array{Int64,1}:
2
3
4
5
For completeness, there's also a third variant, the RowVector, which results of a transposition:
julia> a'
1×4 RowVector{Int64,Array{Int64,1}}:
2 3 4 5
From this you also can't delete.
Deleteat! is only defined for:
Fully implemented by:
Vector (a.k.a. 1-dimensional Array)
BitVector (a.k.a. 1-dimensional BitArray)
A Row Vector (2 Dimensions) won't work.
But ... there is a workaround by this trick:
julia> deleteat!(a[1,:], 1)' # mind the ' -> transposes it back to a row vector.
1×4 RowVector{Int64,Array{Int64,1}}:
2 3 4 5
Ofcourse this wouldn't work for an Array with 2 or more rows.