Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

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
question from:https://stackoverflow.com/questions/65899184/filter-an-array-by-another-array-in-julia

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...