Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to create an array consists of indices of elements in one array that exists in the other array?

$
0
0

@QUANG_VU_NGUY_N_PH_M wrote:

I firstly declared 2 arrays:

coord1 = [[0.0, 0.0],
          [1.0, 0.0],
          [0.0, 1.0],
          [1.0, 1.0],
          [0.0, 2.0],
          [1.0, 2.0]]
 
 coord2 =  [[1.0, 0.0],
            [2.0, 0.0],
            [1.0, 1.0],
            [2.0, 1.0]]

I would like to create

  1. an array index1 that consists of indice of elements in coord2 that exist in coord1
  2. an array index2 that consists of indice of elements in coord1 that exist in coord2

I create the below code :

index1 = []
index2 = []

for (index, value) in enumerate(coord2)
    if value in coord1
        a = findfirst(x->x==value, coord1)
        push!(index1,  a)
        push!(index2,  index)
    end
end

Is there any better way towards this problem?

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles