How to fill an array by row in Julia - arrays

I would like to fill an Array object by row in the Julia language.
The reshape function wants to fill by column (Julia is column major).
julia> reshape(1:15, 3,5)
3x5 Array{Int64,2}:
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
Is there a way to persuade it to fill by row? It feels like there should be an obvious answer, but I've not found one.

One suggestion:
julia> reshape(1:15, 5, 3) |> transpose
3x5 Array{Int64,2}:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

With array comprehension:
julia> [i+5*j for j=0:2,i=1:5]
3x5 Array{Int64,2}:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Ah, it's just more than 10x times faster than other suggestion (actually, an embarrassing 100x on my initial benchmark).

permutedims is another choice when dealing with more general multi-way arrays.
julia> permutedims(reshape(1:24, 2,3,4), [2,1,3])
3x2x4 Array{Int64,3}:
[:, :, 1] =
1 2
3 4
5 6
[:, :, 2] =
7 8
9 10
11 12
[:, :, 3] =
13 14
15 16
17 18
[:, :, 4] =
19 20
21 22
23 24
however, it's slowest among other suggestions in your specific case.

Related

filter an array by another array in julia

I have two arrays in Julia, The array 1 (45807x2) has two columns, first column is for position of snp and second colunm is for snpID, now I want to the snpIDs that has position in array2(4580x1) from array 1.
for example, the first element (5) in array 1 is the fifth snpID (BTA-34880) in array 1.
how can I do it ? thanks.
45807×2 Array{Any,2}:
1 "BovineHD0100000015"
2 "Hapmap43437-BTA-101873"
3 "BovineHD0100000062"
4 "ARS-BFGL-NGS-16466"
5 "BTA-34880"
6 "BovineHD0100000096"
7 "Hapmap34944-BES1_Contig627_1906"
8 "ARS-BFGL-NGS-98142"
9 "rs29015850"
10 "ARS-BFGL-NGS-114208"
11 "ARS-BFGL-NGS-66449"
12 "BovineHD0100000204"
13 "BovineHD0100000220"
⋮
4580-element Array{Int64,1}:
5
6
18
25
26
54
55
67
69
84
88
You can directly use the second array as an index for the first array. Look at this example:
julia> using Random
julia> a = hcat(1:10, shuffle(1:10))
10×2 Array{Int64,2}:
1 7
2 6
3 10
4 1
5 9
6 8
7 4
8 5
9 3
10 2
julia> b = shuffle(1:5)
5-element Array{Int64,1}:
2
5
3
4
1
julia> a[b,2]
5-element Array{Int64,1}:
6
9
10
1
7

Is there a way to rotate 3D arrays in Julia?

I am trying to rotate a 3D array in julia as if it represents a physical object in 3D space. Essentially, I want to know if there is a way to rotate an array by increments of 90 degrees along the x-, y-, and/or the z-axis.
In 2D it would be something like this if I were to rotate counter-clockwise...
1 2 3 3 6 9
4 5 6 -----> 2 5 8
7 8 9 1 4 7
I would want the same logic to apply in 3D as well.
Any help is appreciated.
For two-dimensional Matrices you have functions such as rotl90, rotr90 and rot180. Those can be combined with mapslices to get the desired effect. For an example below is a rotation over dimensions 1 and 2 for each cut of array in the dimension 3.
julia> A=collect(reshape(1:27,3,3,3))
3×3×3 Array{Int64,3}:
[:, :, 1] =
1 4 7
2 5 8
3 6 9
[:, :, 2] =
10 13 16
11 14 17
12 15 18
[:, :, 3] =
19 22 25
20 23 26
21 24 27
julia> mapslices(rotr90,A,dims=[1,2])
3×3×3 Array{Int64,3}:
[:, :, 1] =
3 2 1
6 5 4
9 8 7
[:, :, 2] =
12 11 10
15 14 13
18 17 16
[:, :, 3] =
21 20 19
24 23 22
27 26 25

Julia: join two matrices using the same memory

I want to fuse two arrays without using more memory, it's posible?, for instance:
a=[1 2 3
4 5 6
7 8 9]
b=[11 12 13
14 15 16
17 18 19]
I need to get the array:
c=[a b]
but using the same memory as a and b, i.e, any change in a or b must be reflected in c.
There's also another package CatViews.jl
julia> x = CatView(a, b); # no copying!!!
julia> reshape(x, size(a, 1), :)
3×6 reshape(::CatView{2,Int64}, 3, 6) with eltype Int64:
1 2 3 11 12 13
4 5 6 14 15 16
7 8 9 17 18 19
If you start in reverse, define C first
julia> C = rand(0:9, 3, 6)
3×6 Array{Int64,2}:
3 2 4 4 9 8
8 8 6 5 5 9
0 7 5 8 7 5
then have A and B be views of C
julia> A = #view C[:, 1:3]
3×3 view(::Array{Int64,2}, :, 1:3) with eltype Int64:
3 2 4
8 8 6
0 7 5
julia> B = #view C[:, 4:6]
3×3 view(::Array{Int64,2}, :, 4:6) with eltype Int64:
4 9 8
5 5 9
8 7 5
then it works.
julia> A[2,2] = -1
-1
julia> C
3×6 Array{Int64,2}:
3 2 4 4 9 8
8 -1 6 5 5 9
0 7 5 8 7 5

Julia: delete rows and columns from an array or matix

How can I delete one or more rows and/or columns from an array?
Working with:
julia> array = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
4×4 Array{Int64,2}:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
To delete a single row (here row 2):
julia> newarray = array[1:end .!= 2, :]
3×4 Array{Int64,2}:
1 2 3 4
9 10 11 12
13 14 15 16
To delete a single column (here column 3):
julia> newarray = array[:, 1:end .!= 3]
4×3 Array{Int64,2}:
1 2 4
5 6 8
9 10 12
13 14 16
To delete a single row and a single column (here row 2, column 3):
julia> newarray = array[1:end .!= 3, 1:end .!= 3]
3×3 Array{Int64,2}:
1 2 4
5 6 8
13 14 16
To delete multiple rows (here rows 2, 4):
julia> newarray = array[setdiff(1:end, (2,4)), :]
2×4 Array{Int64,2}:
1 2 3 4
9 10 11 12
To delete multiple columns (here columns 2, 4):
julia> newarray = array[:, setdiff(1:end, (2,4))]
4×2 Array{Int64,2}:
1 3
5 7
9 11
13 15
To delete a single row and multiple columns (here row 4 and columns 3, 4):
julia> newarray = array[1:end .!= 4, setdiff(1:end, (3,4))]
3×2 Array{Int64,2}:
1 2
5 6
9 10
# or
julia> newarray = array[setdiff(1:end, 4), setdiff(1:end, (3,4))]
3×2 Array{Int64,2}:
1 2
5 6
9 10
# or
julia> newarray = array[setdiff(1:end, (4,)), setdiff(1:end, (3,4))]
3×2 Array{Int64,2}:
1 2
5 6
9 10
To delete multiple rows and columns (here rows 1, 2 and columns 3, 4):
julia> newarray = array[setdiff(1:end, (1,2)), setdiff(1:end, (3,4))]
2×2 Array{Int64,2}:
9 10
13 14

What is the meaning of the addition at the end of this array declaration?

I'm tasked with implementing an algorithm which was supplied as Matlab (which none of us have any experience with) into our c++ application.
There is an array declared as such:
encrypted = [18 10 20 13 6 25 21 13 17;
2 26 4 29 22 9 5 29 1;
19 11 21 12 7 24 20 12 16;
% ... many rows like this ...
13 21 11 18 25 6 10 18 14]+1;
What is the semantic meaning of the +1 at the end of the array declaration?
Simply adding 1 to each entry:
>> [1 2 3; 4 5 6]
ans =
1 2 3
4 5 6
>> [1 2 3; 4 5 6] + 1
ans =
2 3 4
5 6 7
If you have MATLAB around, you could have figured that out by just trying. If you do not, I hope you have a very clear picture of what the code is doing and write a good test suite, since you won't be able to compare your new code's output to the MATLAB one.
The +1 means that all elements of the written matrix will be increased by one.
Example
out = [1 2;
3 4] + 1;
disp(out)
2 3
4 5

Resources