How to transpose the 1st and -1th levels of arbitrarily nested array? The Next CEO of Stack OverflowEmulating R data frame getters with UpValuesQuickly pruning elements in one structured array that exist in a separate unordered arrayJoining sub-lists nested two levels deep in a list`Part` like `Delete`: How to delete list of columns or arbitrarily deeper levelsHow to mesh a region using adaptive cubic elementsHow to efficiently Flatten nested lists while preserving select levels?Distribute elements of one line across arbitrary dimension of another listDeep level nested list addition`Transpose` nested `Association`Reformatting the pattern inside a list
Is it ever safe to open a suspicious html file (e.g. email attachment)?
Is there a way to save my career from absolute disaster?
Skipping indices in a product
How to start emacs in "nothing" mode (`fundamental-mode`)
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
MessageLevel in QGIS3
Written every which way
How to make a variable always equal to the result of some calculations?
Extending anchors in TikZ
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
What can we do to stop prior company from asking us questions?
Do I need to enable Dev Hub in my PROD Org?
What's the best way to handle refactoring a big file?
How do we know the LHC results are robust?
Return the Closest Prime Number
What does convergence in distribution "in the Gromov–Hausdorff" sense mean?
What happened in Rome, when the western empire "fell"?
Why do remote companies require working in the US?
If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?
Interfacing a button to MCU (and PC) with 50m long cable
Why don't programming languages automatically manage the synchronous/asynchronous problem?
Elegant way to replace substring in a regex with optional groups in Python?
How does the mv command work with external drives?
Contours of a clandestine nature
How to transpose the 1st and -1th levels of arbitrarily nested array?
The Next CEO of Stack OverflowEmulating R data frame getters with UpValuesQuickly pruning elements in one structured array that exist in a separate unordered arrayJoining sub-lists nested two levels deep in a list`Part` like `Delete`: How to delete list of columns or arbitrarily deeper levelsHow to mesh a region using adaptive cubic elementsHow to efficiently Flatten nested lists while preserving select levels?Distribute elements of one line across arbitrary dimension of another listDeep level nested list addition`Transpose` nested `Association`Reformatting the pattern inside a list
$begingroup$
Is there a straightforward way to convert
arr =
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;
to:
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
?
So I need to swap the first and last dimension. Which should in principle be possible because altough arr does not have a fixed structure the 'bottom' is always uniform:
Level[arr, -2]
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
Had Transpose/Flatten/MapThread accepted negative level specification it would have been easy. That is not the case.
One can think about that question as: how to create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]].
EDIT:
In general Level[arr, -2] should be a rectangular array but rows do not need to be the same.
list-manipulation
$endgroup$
add a comment |
$begingroup$
Is there a straightforward way to convert
arr =
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;
to:
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
?
So I need to swap the first and last dimension. Which should in principle be possible because altough arr does not have a fixed structure the 'bottom' is always uniform:
Level[arr, -2]
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
Had Transpose/Flatten/MapThread accepted negative level specification it would have been easy. That is not the case.
One can think about that question as: how to create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]].
EDIT:
In general Level[arr, -2] should be a rectangular array but rows do not need to be the same.
list-manipulation
$endgroup$
$begingroup$
Not a solution, butFlatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though:SparseArraydoes not construct ragged structures.
$endgroup$
– Roman
1 hour ago
$begingroup$
Maybe something along the lines ofarr /. a,b->a,a,b->b? Or perhaps more generally,arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
$endgroup$
– Carl Woll
1 hour ago
$begingroup$
Does your list always containa,bat the lowest level, or can there be anything there as long as they're all of same length?
$endgroup$
– Roman
1 hour ago
$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
1 hour ago
add a comment |
$begingroup$
Is there a straightforward way to convert
arr =
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;
to:
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
?
So I need to swap the first and last dimension. Which should in principle be possible because altough arr does not have a fixed structure the 'bottom' is always uniform:
Level[arr, -2]
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
Had Transpose/Flatten/MapThread accepted negative level specification it would have been easy. That is not the case.
One can think about that question as: how to create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]].
EDIT:
In general Level[arr, -2] should be a rectangular array but rows do not need to be the same.
list-manipulation
$endgroup$
Is there a straightforward way to convert
arr =
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;
to:
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
?
So I need to swap the first and last dimension. Which should in principle be possible because altough arr does not have a fixed structure the 'bottom' is always uniform:
Level[arr, -2]
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
Had Transpose/Flatten/MapThread accepted negative level specification it would have been easy. That is not the case.
One can think about that question as: how to create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]].
EDIT:
In general Level[arr, -2] should be a rectangular array but rows do not need to be the same.
list-manipulation
list-manipulation
edited 1 hour ago
Kuba
asked 1 hour ago
Kuba♦Kuba
107k12210531
107k12210531
$begingroup$
Not a solution, butFlatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though:SparseArraydoes not construct ragged structures.
$endgroup$
– Roman
1 hour ago
$begingroup$
Maybe something along the lines ofarr /. a,b->a,a,b->b? Or perhaps more generally,arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
$endgroup$
– Carl Woll
1 hour ago
$begingroup$
Does your list always containa,bat the lowest level, or can there be anything there as long as they're all of same length?
$endgroup$
– Roman
1 hour ago
$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
1 hour ago
add a comment |
$begingroup$
Not a solution, butFlatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though:SparseArraydoes not construct ragged structures.
$endgroup$
– Roman
1 hour ago
$begingroup$
Maybe something along the lines ofarr /. a,b->a,a,b->b? Or perhaps more generally,arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
$endgroup$
– Carl Woll
1 hour ago
$begingroup$
Does your list always containa,bat the lowest level, or can there be anything there as long as they're all of same length?
$endgroup$
– Roman
1 hour ago
$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
1 hour ago
$begingroup$
Not a solution, but
Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.$endgroup$
– Roman
1 hour ago
$begingroup$
Not a solution, but
Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.$endgroup$
– Roman
1 hour ago
$begingroup$
Maybe something along the lines of
arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?$endgroup$
– Carl Woll
1 hour ago
$begingroup$
Maybe something along the lines of
arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?$endgroup$
– Carl Woll
1 hour ago
$begingroup$
Does your list always contain
a,b at the lowest level, or can there be anything there as long as they're all of same length?$endgroup$
– Roman
1 hour ago
$begingroup$
Does your list always contain
a,b at the lowest level, or can there be anything there as long as they're all of same length?$endgroup$
– Roman
1 hour ago
$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
1 hour ago
$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
SetAttributes[f1, Listable]
Apply[f1, arr, 0, -3] /. f1 -> List
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
add a comment |
$begingroup$
This is what the list at the lowest level looks like:
el = First@Level[list, -2];
Using this, we can solve it with a rules-based approach:
list /. el -> # & /@ el
or a recursive approach like this:
walk[lists : __List, i_] := walk[#, i] & /@ lists
walk[atoms : __, i_] := i
walk[list, #] & /@ el
$endgroup$
add a comment |
$begingroup$
Terrible solution using Table but works:
Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194206%2fhow-to-transpose-the-1st-and-1th-levels-of-arbitrarily-nested-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
SetAttributes[f1, Listable]
Apply[f1, arr, 0, -3] /. f1 -> List
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
add a comment |
$begingroup$
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
SetAttributes[f1, Listable]
Apply[f1, arr, 0, -3] /. f1 -> List
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
add a comment |
$begingroup$
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
SetAttributes[f1, Listable]
Apply[f1, arr, 0, -3] /. f1 -> List
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
SetAttributes[f1, Listable]
Apply[f1, arr, 0, -3] /. f1 -> List
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
answered 1 hour ago
andre314andre314
12.2k12352
12.2k12352
add a comment |
add a comment |
$begingroup$
This is what the list at the lowest level looks like:
el = First@Level[list, -2];
Using this, we can solve it with a rules-based approach:
list /. el -> # & /@ el
or a recursive approach like this:
walk[lists : __List, i_] := walk[#, i] & /@ lists
walk[atoms : __, i_] := i
walk[list, #] & /@ el
$endgroup$
add a comment |
$begingroup$
This is what the list at the lowest level looks like:
el = First@Level[list, -2];
Using this, we can solve it with a rules-based approach:
list /. el -> # & /@ el
or a recursive approach like this:
walk[lists : __List, i_] := walk[#, i] & /@ lists
walk[atoms : __, i_] := i
walk[list, #] & /@ el
$endgroup$
add a comment |
$begingroup$
This is what the list at the lowest level looks like:
el = First@Level[list, -2];
Using this, we can solve it with a rules-based approach:
list /. el -> # & /@ el
or a recursive approach like this:
walk[lists : __List, i_] := walk[#, i] & /@ lists
walk[atoms : __, i_] := i
walk[list, #] & /@ el
$endgroup$
This is what the list at the lowest level looks like:
el = First@Level[list, -2];
Using this, we can solve it with a rules-based approach:
list /. el -> # & /@ el
or a recursive approach like this:
walk[lists : __List, i_] := walk[#, i] & /@ lists
walk[atoms : __, i_] := i
walk[list, #] & /@ el
answered 1 hour ago
C. E.C. E.
50.8k399205
50.8k399205
add a comment |
add a comment |
$begingroup$
Terrible solution using Table but works:
Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]
$endgroup$
add a comment |
$begingroup$
Terrible solution using Table but works:
Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]
$endgroup$
add a comment |
$begingroup$
Terrible solution using Table but works:
Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]
$endgroup$
Terrible solution using Table but works:
Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]
answered 50 mins ago
RomanRoman
3,9361022
3,9361022
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194206%2fhow-to-transpose-the-1st-and-1th-levels-of-arbitrarily-nested-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Not a solution, but
Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though:SparseArraydoes not construct ragged structures.$endgroup$
– Roman
1 hour ago
$begingroup$
Maybe something along the lines of
arr /. a,b->a,a,b->b? Or perhaps more generally,arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?$endgroup$
– Carl Woll
1 hour ago
$begingroup$
Does your list always contain
a,bat the lowest level, or can there be anything there as long as they're all of same length?$endgroup$
– Roman
1 hour ago
$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba♦
1 hour ago
$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
1 hour ago