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
Related
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
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
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.
I have a simple matrix:
1 2 3 4
5 6 7 8
8 9 10 11
12 13 14 15
I need to loop through each element and build a new matrix with 3 of its surrounding elements (the one to the right, bottom right and bottom). So I will end up with an array like so:
1 2 6 5
2 3 7 6
3 4 8 7
I managed to do this but when I need to jump to the row below I can't seem to figure out how to do it. for the next row it should be:
5 6 9 8
6 7 10 9
...
Any ideas?
[m n] = size(A);
[jj ii] = ndgrid(1:m-1, 1:n-1); %// rows and columns except last ones
kk = sub2ind([m n], ii(:),jj(:)); %// to linear index
B = [ A(kk) A(kk+m) A(kk+m+1) A(kk+1) ] %// pick desired values with linear index
In your example:
B =
1 2 6 5
2 3 7 6
3 4 8 7
5 6 9 8
6 7 10 9
7 8 11 10
8 9 13 12
9 10 14 13
10 11 15 14
My favourite bsxfun being put to work here -
[M,N] = size(A); %// A is Input
ind = bsxfun(#plus,[1:M-1],[(0:N-2).*M]') %//'
out = A(bsxfun(#plus,ind(:),[0 M M+1 1])) %// Desired output
Output using the sample input from question -
out =
1 2 6 5
2 3 7 6
3 4 8 7
5 6 9 8
6 7 10 9
7 8 11 10
8 9 13 12
9 10 14 13
10 11 15 14
I would like to split a matrix which has two columns into an array. Everything I have tested so far splits it by column, e.g.
mat <- rbind(c(5, 9),
c(3, 7),
c(2, 1),
c(4, 3),
c(8, 6))
ind <- gl(1,10)
>split(mat, ind)
[1] 5 3 2 4 8 9 7 1 3 6
But the desired output is:
5 9 3 7 2 1 4 3 8 6
There must be a super easy neat trick to do this. Any pointers are highly appreciated, thanks!
You can just use as.vector:
## what you presently have
as.vector(mat)
[1] 5 3 2 4 8 9 7 1 3 6
## What you are looking for
as.vector(t(mat))
# [1] 5 9 3 7 2 1 4 3 8 6