Elegant way of partitioning in two starting from the second element?
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I wrote a code for partitioning a list. I want to group them into two groups following the same pattern: 1. element added, then 2. and 3. are omitted, then 4. and 5. added and goes like this. Partitioning is easy, then I have to add the first element manually but the problem is when I partition the list, it omits the last element because then the list contains odd number of elements and I have to add it to the list manually also. I thought maybe someone can figure out a more elegant way. My list is as follow:
vars= c0r, c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i, c6r,
c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i, c11r, c11i, c12r,
c12i, c13r, c13i, c14r, c14i, c15r, c15i
and here is my code;
varr1 = Partition[vars[[2 ;;]], 2]
c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i,
c6r, c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i,
c11r, c11i, c12r, c12i, c13r, c13i, c14r, c14i, c15r
Join[vars[[1]], Flatten[varr1[[2 ;; ;; 2]]], vars[[-1]]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
and this is the list I want.
list-manipulation
add a comment |Â
up vote
4
down vote
favorite
I wrote a code for partitioning a list. I want to group them into two groups following the same pattern: 1. element added, then 2. and 3. are omitted, then 4. and 5. added and goes like this. Partitioning is easy, then I have to add the first element manually but the problem is when I partition the list, it omits the last element because then the list contains odd number of elements and I have to add it to the list manually also. I thought maybe someone can figure out a more elegant way. My list is as follow:
vars= c0r, c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i, c6r,
c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i, c11r, c11i, c12r,
c12i, c13r, c13i, c14r, c14i, c15r, c15i
and here is my code;
varr1 = Partition[vars[[2 ;;]], 2]
c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i,
c6r, c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i,
c11r, c11i, c12r, c12i, c13r, c13i, c14r, c14i, c15r
Join[vars[[1]], Flatten[varr1[[2 ;; ;; 2]]], vars[[-1]]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
and this is the list I want.
list-manipulation
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I wrote a code for partitioning a list. I want to group them into two groups following the same pattern: 1. element added, then 2. and 3. are omitted, then 4. and 5. added and goes like this. Partitioning is easy, then I have to add the first element manually but the problem is when I partition the list, it omits the last element because then the list contains odd number of elements and I have to add it to the list manually also. I thought maybe someone can figure out a more elegant way. My list is as follow:
vars= c0r, c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i, c6r,
c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i, c11r, c11i, c12r,
c12i, c13r, c13i, c14r, c14i, c15r, c15i
and here is my code;
varr1 = Partition[vars[[2 ;;]], 2]
c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i,
c6r, c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i,
c11r, c11i, c12r, c12i, c13r, c13i, c14r, c14i, c15r
Join[vars[[1]], Flatten[varr1[[2 ;; ;; 2]]], vars[[-1]]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
and this is the list I want.
list-manipulation
I wrote a code for partitioning a list. I want to group them into two groups following the same pattern: 1. element added, then 2. and 3. are omitted, then 4. and 5. added and goes like this. Partitioning is easy, then I have to add the first element manually but the problem is when I partition the list, it omits the last element because then the list contains odd number of elements and I have to add it to the list manually also. I thought maybe someone can figure out a more elegant way. My list is as follow:
vars= c0r, c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i, c6r,
c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i, c11r, c11i, c12r,
c12i, c13r, c13i, c14r, c14i, c15r, c15i
and here is my code;
varr1 = Partition[vars[[2 ;;]], 2]
c0i, c1r, c1i, c2r, c2i, c3r, c3i, c4r, c4i, c5r, c5i,
c6r, c6i, c7r, c7i, c8r, c8i, c9r, c9i, c10r, c10i,
c11r, c11i, c12r, c12i, c13r, c13i, c14r, c14i, c15r
Join[vars[[1]], Flatten[varr1[[2 ;; ;; 2]]], vars[[-1]]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
and this is the list I want.
list-manipulation
list-manipulation
edited 2 hours ago
asked 2 hours ago
Buddha_the_Scientist
6715
6715
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You can use the five-argument form of Partition
:
Flatten[Partition[vars, 2, 4, -1, 1, ]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
You can also use
Pick[#, Mod[Range @ Length @ #, 4], 1 | 0]& @ vars
Flatten[Partition[vars, 4][[All, 1, -1]]]
Flatten[List @@@ ListConvolve[1, 0, 0, 1, vars][[ ;; ;; 4]]]
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You can use the five-argument form of Partition
:
Flatten[Partition[vars, 2, 4, -1, 1, ]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
You can also use
Pick[#, Mod[Range @ Length @ #, 4], 1 | 0]& @ vars
Flatten[Partition[vars, 4][[All, 1, -1]]]
Flatten[List @@@ ListConvolve[1, 0, 0, 1, vars][[ ;; ;; 4]]]
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
add a comment |Â
up vote
3
down vote
accepted
You can use the five-argument form of Partition
:
Flatten[Partition[vars, 2, 4, -1, 1, ]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
You can also use
Pick[#, Mod[Range @ Length @ #, 4], 1 | 0]& @ vars
Flatten[Partition[vars, 4][[All, 1, -1]]]
Flatten[List @@@ ListConvolve[1, 0, 0, 1, vars][[ ;; ;; 4]]]
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You can use the five-argument form of Partition
:
Flatten[Partition[vars, 2, 4, -1, 1, ]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
You can also use
Pick[#, Mod[Range @ Length @ #, 4], 1 | 0]& @ vars
Flatten[Partition[vars, 4][[All, 1, -1]]]
Flatten[List @@@ ListConvolve[1, 0, 0, 1, vars][[ ;; ;; 4]]]
You can use the five-argument form of Partition
:
Flatten[Partition[vars, 2, 4, -1, 1, ]]
c0r, c1i, c2r, c3i, c4r, c5i, c6r, c7i, c8r, c9i, c10r, c11i, c12r,
c13i, c14r, c15i
You can also use
Pick[#, Mod[Range @ Length @ #, 4], 1 | 0]& @ vars
Flatten[Partition[vars, 4][[All, 1, -1]]]
Flatten[List @@@ ListConvolve[1, 0, 0, 1, vars][[ ;; ;; 4]]]
edited 1 hour ago
answered 1 hour ago
kglr
164k8188388
164k8188388
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
add a comment |Â
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
You find the more clever way of picking them as first and third.
â Buddha_the_Scientist
1 hour ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183538%2felegant-way-of-partitioning-in-two-starting-from-the-second-element%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password