idea of conditional arrangement

Asked 2 years ago, Updated 2 years ago, 118 views

Create a new array from the following two arrays.

common_y=[1,2]
new_y = [11, 12, 13, 14, 15, 16, 18, 19, 20, 22, 23, 24, 25, 26, 27]

If you have a good idea that doesn't work out well, I'd like to ask you a question.

Conditions
When creating a new array, the purpose is to copy the new_y while excluding the element of the new_y array number that corresponds to the element of common_y.It is written in a generalized manner so that we can respond to changes in the arrangement of common_y.
What I'm thinking about now is that I don't want to put things that don't fit into an array under if conditions, but I need to change the variables because the lengths of each array are different, but I'm worried about outputting unnecessary results when the variables are at a certain value.

new_new_y=Int64[]
for i in minimum(new_y): maximum(new_y)
    for jin1: length(common_y)
        if i == new_y [common_y[j]]
            continue
        else
            push!(new_new_y,i)
        end
    end
end
new_new_y

The other thing is that I was going to write using splice!, but if I turn it around with a for sentence, the length of the array changes every time I turn it, and I can't get the result.

for in 1:length(common_y)
        splice!(new_y,common_y[i])
end

It's simple, but it's quite difficult and I'm struggling.
If you have any advice, please let me know.
Thank you for your cooperation

array julia

2022-09-30 15:34

1 Answers

With the InvertedIndices package, you don't have to write too much code.

using InvertedIndices

common_y=[1,2]
new_y = [11, 12, 13, 14, 15, 16, 17, 18, ,
         19, 20, 21, 22, 23, 24, 25, 26, 27]

new_new_y = new_y [Not(common_y)]


2022-09-30 15:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.