0

this code is a small example of what i want to do, i have this small example right here:

y=[[[1,0],[0,0],[1,1],[1,0],[1,0],[0,1]],[[1,0],[0,0],[1,1],[0,0]],[[1,0],[0,0],[1,1],[1,0],[1,0],[1,0]]]
array=[]
all_array=[]
for i in 1:length(y)
    for j in 1: length(y[i])
        if y[i][j]==[1,0]
            push!(array,y[i][j])
        end
        
    end
end

what i'm expecting when you return array is this :

[[[1,0],[1,0],[1,0]],[[1,0]],[[1,0],[1,0],[1,0],[1,0]]]

which is 3-element Array{Array{Array{Int64,1},1},1} but instead i get this:

8-element Array{Any,1}:
 [1, 0]
 [1, 0]
 [1, 0]
 [1, 0]
 [1, 0]
 [1, 0]
 [1, 0]
 [1, 0]

i don't know what i'm missing here.

1 Answer 1

1

You are pushing all the elements that satisfy your condition one after another in array. You could create your array instead like:

julia> array=[[],[],[]]

And modify your loop to push! into each subarray array[i] like this:

julia> for i in 1:length(y)
           for j in 1: length(y[i])
               if y[i][j]==[1,0]
                   push!(array[i],y[i][j])
               end

           end
       end

Which then gives you your desired array.

julia> array
3-element Array{Array{Any,1},1}:
 [[1, 0], [1, 0], [1, 0]]
 [[1, 0]]
 [[1, 0], [1, 0], [1, 0], [1, 0]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.