I have a 2x2 matrix
a = [1 2
3 4]
2×2 Array{Int64,2}:
1 2
3 4
and want to add a row of all 0s. I can just do this:
vcat(0, a)
3×2 Array{Int64,2}:
0 0
1 2
3 4
The 0 gets recycled and fills up the entire first row. To me (coming from R) this makes sense.
What does not make sense to me is the following:
hcat(0, a)
ERROR: DimensionMismatch("mismatch in dimension 1 (expected 1 got 2)")
Anyone got an explanation?
To reiterate what Matt and Bogumil said, Julia does not support recycling, it's a bug: https://github.com/JuliaLang/julia/issues/38019
Related
I have a 4: 3 matrix and I would like to take only 2: 3.
Is it possible to delimit using the DelimitedFiles command?
How should I use it?
Q = convert(Matrix, ACT[2:3])#I would like to omit the 2:3
Suppose you have the following Matrix:
julia> a = collect(reshape(1:12,(4,3)))
4×3 Array{Int64,2}:
1 5 9
2 6 10
3 7 11
4 8 12
You can select a subarray in two ways - either making a copy of the desired part or creating a view.
Making a copy (a new object is created and Array's data is copied:
julia> a[1:2,:]
2×3 Array{Int64,2}:
1 5 9
2 6 10
Creating a view. This is in many scenarios much faster because the data is not copied:
julia> view(a, 1:2, :)
2×3 view(::Array{Int64,2}, 1:2, :) with eltype Int64:
1 5 9
2 6 10
For creating the view you can also use a macro - in this case your code will be more similar to the one that copies the data:
julia> #view a[1:2,:]
2×3 view(::Array{Int64,2}, 1:2, :) with eltype Int64:
1 5 9
2 6 10
Each of those views can be shown using the command display and this is the standard way to handle showing them.
However if you want to use DelmitedFiles you can, for example:
julia> using DelimitedFiles;writedlm(stdout, #view a[1:2,:])
1 5 9
2 6 10
I try to "combine" multiple sequenzes like 'large numeric' or 'large char' to a single 'large numeric' or 'large char' sequence while keeping the duplicates of the combined object elements and not changing the order of elements.
union() does almost what i want
x <- c(0,1,6,2,3,4,5)
y <- c(6,0,0,1,3,0,4,5,1,3,-1)
z <- union(x,y)
z
#results in
#[1] 0 1 6 2 3 4 5 -1
#but what i need is:
#[1] 0 1 6 2 3 4 5 6 0 0 1 3 0 4 5 1 3 -1
Since x and y become huge (up to millions of values) a loop attempt might fail due to computation time.
In R there are a lot of functions to combine all kinds of data; therefore hours of search yielded not what i needed, but the solution can´t be so hard to find (frustration)
We can use concatenate
c(x, y)
I have a matrix A with A[:,1] as Bus_id. So Bus_id are 1,3,4, and 6. For processing, I equated Bus_id's to consecutive row indexing, see A_new matrix.
julia> A=[1 1 3;3 1 1; 4 1 7;6 1 1]
4×3 Array{Int64,2}:
1 1 3
3 1 1
4 1 7
6 1 1
julia> A_new
1 1 1
2 1 1
3 1 1
4 1 1
Now, I have another matrix B, which has some elements of matrix A. I wish to convert B matrix's bus_ids to b_new. I don't know how to explain this problem.
julia> B= [3 1 1; 4 1 7]
2×3 Array{Int64,2}:
3 1 1
6 1 1
julia> B_new
2 1 1
4 1 7
I have tried masking by it works only for one element.
Please help me find a way.
It is possible that you are using Bus_id as an index. If you want to renumber the business ID's, but not lose track of transactions indexed with the original business id's, what you want to do fits naturally into a Dict that translates the Bus_id from one to another.
One problem that immediately arises is what should happen if some of the entries in B have no translation from A, but are already set to a number that is in A's new key? Potential cross-linked database chaos! Instead, the new ids need to be unique if at all possible! I suggest making them negative.
If you use matrix A as your key to translation (and assuming that all entries in A[:,1] are unique--if not the logic might need to drop duplicates first) the dict usage then looks like this:
A = [1 1 3; 3 1 1; 4 1 7; 6 1 1]
B = [3 1 1; 6 1 1]
function consecutive_row_indexing(mat)
dict = Dict{Int, Int}()
for (i,n) in enumerate(mat[:,1])
dict[n] = -i
end
dict
end
function renumberbus_ids!(mat, dict)
for i in 1:size(mat)[1]
if haskey(dict, mat[i,1])
mat[i,1] = dict[mat[i,1]]
end
end
mat
end
d = consecutive_row_indexing(A)
println(renumberbus_ids!(A, d))
println(renumberbus_ids!(B, d))
output: <code>
[-1 1 3; -2 1 1; -3 1 7; -4 1 1]
[-2 1 1; -4 1 1]
If you still really want your B matrix with positive integers for its index column, just replace = -i with = i on the seventh line of the code above.
If I want to create 2D array with 1 row by 5 columns.
I could do this
julia> a = [1 2 3 4 5]
1×5 Array{Int64,2}:
1 2 3 4 5
But to create 2D array with 5 rows by 1 column. I have tried
julia> b = [1; 2; 3; 4; 5]
5-element Array{Int64,1}:
1
2
3
4
5
But I got back a 1D array which is NOT what I wanted
The only way to get it to work is
julia> b=reshape([1 2 3 4 5],5,1)
5×1 Array{Int64,2}:
1
2
3
4
5
Perhaps I am missing some crucial information here.
You could also do a = [1 2 3 4 5]'.
On a side note, for Julia versions > 0.6 the type of a wouldn't be Array{Int64, 2} but a LinearAlgebra.Adjoint{Int64,Array{Int64,2}} as conjugate transpose is lazy in this case. One can get <= 0.6 behavior by a = copy([1 2 3 4 5]').
AFAIK there is no syntactic sugar for it.
I usually write:
hcat([1, 2, 3, 4, 5])
which is short and I find it easy to remember.
If you use reshape you can replace one dimension with : which means you do not have to count (it is useful e.g. when you get an input vector as a variable):
reshape([1 2 3 4 5], :, 1)
Finally you could use:
permutedims([1 2 3 4 5])
julia> display([i*j for i=1:3, j=1:3])
3×3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
julia> display([i*j for i=1:3, j=1:3 i>=j])
6-element Array{Int64,1}:
1
2
3
4
6
9
not a surprise. what i'd like is:
3×3 Array{Int64,2}:
1
2 4
3 6 9
i suppose a for loop is needed. what i don't want is to generate the entire array and then filter out or replace the ones.
while the example is symmetric, it not really relevant to the q. any f(i,j) could be substituted for i*j. (symmetric or not)
I suppose you wanted to write [i*j for i=1:3, j=1:3 if i>=j]. The if condition will always make your result a vector.
What you can do to avoid generating an entire array is e.g.:
x = Matrix{Int}(3,3)
for i in 1:3, j in 1:i
x[i,j] = i*j
end
y = LowerTriangular(x)