How to limit the columns of an array in Julia - julia

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

Related

How can I load a Vector into a single column of an Array in Julia

That's it. My program generates a vector of Int64s, and each time I need to stack each Vector into a M by N Matrix
Everything. e.g.
Push, Append, hcat [], (), {}, even ¯_(ツ)_/¯
If this is a new matrix use hcat:
julia> hcat([1,2,3],[4,5,6])
3×2 Matrix{Int64}:
1 4
2 5
3 6
This will also work with vector of vectors:
julia> vecs = [[1,2,3],[4,5,6],[7,8,9]];
julia> a = hcat(vecs...)
3×3 Matrix{Int64}:
1 4 7
2 5 8
3 6 9
If performance is an issue reduce combined with hcat will be more verbose but much faster:
reduce(hcat, vecs)
To put data into a column use broadcasted assignment:
julia> a[:,2] .= [40,50,60];
julia> a
3×3 Matrix{Int64}:
1 40 7
2 50 8
3 60 9

How to filter map like array comprehension?

With arrays, you can filter based on a condition:
[i for i=1:10 if isodd(i) ]
returns:
5-element Vector{Int64}:
1
3
5
7
9
but attempting something similar with map results in nothing values:
julia> map(1:10) do i
isodd(i) ? i : nothing
end
reutrns:
10-element Vector{Union{Nothing, Int64}}:
1
nothing
3
nothing
5
nothing
7
nothing
9
nothing
map is a one-to-one mapping. So I'm afraid you can't do the same with map. Maybe what you are looking for is just filter?
julia> a = 1:10
1:10
julia> filter(isodd, a)
5-element Vector{Int64}:
1
3
5
7
9

Get 2d index from flattened array

Given the array:
arr = [1 2; 3 4; 5 6]
3×2 Array{Int64,2}:
1 2
3 4
5 6
which is flattened flat_arr = collect(Iterators.flatten(arr))
6-element Array{Int64,1}:
1
3
5
2
4
6
I sometimes need to go between both index formats. For example, if I got the sorted indices of flat_arr, I may want to iterate over arr using these sorted indices. In Python, this is typically done with np.unravel_index. How is this done in Julia? Do I just need to write my own function?
vec() creates a 1-d view of the array. Hence you can have both pointers to the array in the memory and use whichever one you need in any minute (they point to the same array):
julia> arr = [1 2; 3 4; 5 6]
3×2 Array{Int64,2}:
1 2
3 4
5 6
julia> arr1d = vec(arr)
6-element Array{Int64,1}:
1
3
5
2
4
6
julia> arr1d[4] = 99
99
julia> arr
3×2 Array{Int64,2}:
1 99
3 4
5 6
Note that in Julia arrays are stored in column major order and hence the fourth value is the first value in the second column
This can be accomplished using CartesianIndices.
c_i = CartesianIndices(arr)
flat_arr[2] == arr[c_i[2]]) == 3

I want to create 2D array with 5 rows by 1 column

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])

display interaction with Julia list comprehension

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)

Resources