How do I transpose the first and deepest levels of an arbitrarily nested array?How to invert MapIndexed on a ragged structure? How to construct a tree from rules?A question about transforming one List into two Lists with additional requirementsEmulating R data frame getters with UpValuesQuickly pruning elements in one structured array that exist in a separate unordered array`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`How to extract the first element in nested lists
I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine
What is the offset in a seaplane's hull?
Why was the "bread communication" in the arena of Catching Fire left out in the movie?
Symmetry in quantum mechanics
Is it possible to make sharp wind that can cut stuff from afar?
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
How to delete an array properly in Java
Why doesn't a const reference extend the life of a temporary object passed via a function?
Why do UK politicians seemingly ignore opinion polls on Brexit?
Is every set a filtered colimit of finite sets?
Set up public ip on server
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
Is this homebrew feat, Beast of Burden, balanced?
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
Need help identifying/translating a plaque in Tangier, Morocco
Why is "Reports" in the sentence below without the article "The"?
Is there a familial term for apples and pears?
Shall I use personal or official e-mail account when registering to external websites for work purpose?
DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?
Correctly defining the return of a procedure
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
Prime joint compound before latex paint?
How can I add custom success page
Least quadratic residue under GRH: an explicit bound
How do I transpose the first and deepest levels of an arbitrarily nested array?
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?A question about transforming one List into two Lists with additional requirementsEmulating R data frame getters with UpValuesQuickly pruning elements in one structured array that exist in a separate unordered array`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`How to extract the first element in nested lists
$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
?
I need to swap the first and last dimension. Which should in principle be possible, because, although 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 a negative level specification, it would have been easy. That is not the case.
One can think about that question as: How do I 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.
So this:
a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;
should end up:
a1, a2, a3, a4, a5, a6, a7 , ...;
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
?
I need to swap the first and last dimension. Which should in principle be possible, because, although 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 a negative level specification, it would have been easy. That is not the case.
One can think about that question as: How do I 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.
So this:
a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;
should end up:
a1, a2, a3, a4, a5, a6, a7 , ...;
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
Mar 29 at 19:41
$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
Mar 29 at 19:43
$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
Mar 29 at 19:52
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
?
I need to swap the first and last dimension. Which should in principle be possible, because, although 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 a negative level specification, it would have been easy. That is not the case.
One can think about that question as: How do I 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.
So this:
a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;
should end up:
a1, a2, a3, a4, a5, a6, a7 , ...;
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
?
I need to swap the first and last dimension. Which should in principle be possible, because, although 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 a negative level specification, it would have been easy. That is not the case.
One can think about that question as: How do I 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.
So this:
a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;
should end up:
a1, a2, a3, a4, a5, a6, a7 , ...;
list-manipulation
list-manipulation
edited Mar 30 at 5:04
J. M. is away♦
98.9k10311467
98.9k10311467
asked Mar 29 at 19:09
Kuba♦Kuba
107k12211534
107k12211534
$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
Mar 29 at 19:41
$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
Mar 29 at 19:43
$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
Mar 29 at 19:52
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
Mar 29 at 19:41
$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
Mar 29 at 19:43
$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
Mar 29 at 19:52
$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
Mar 29 at 19:41
$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
Mar 29 at 19:41
$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
Mar 29 at 19:43
$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
Mar 29 at 19:43
$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
Mar 29 at 19:52
$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
Mar 29 at 19:52
add a comment |
4 Answers
4
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 |
$begingroup$
This is a bit roundabout but very versatile. First convert the structure arr into a list of rules, then reassemble these rules (after proper modification using RotateRight to transpose first and deepest levels) into a new structure.
Using the toTree function from this answer (thanks to @b3m2a1 !):
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
toTree[Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]]
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$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');
);
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-do-i-transpose-the-first-and-deepest-levels-of-an-arbitrarily-nested-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
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 Mar 29 at 19:53
andre314andre314
12.4k12353
12.4k12353
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 Mar 29 at 19:42
C. E.C. E.
51k3101206
51k3101206
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 Mar 29 at 20:16
RomanRoman
4,61511128
4,61511128
add a comment |
add a comment |
$begingroup$
This is a bit roundabout but very versatile. First convert the structure arr into a list of rules, then reassemble these rules (after proper modification using RotateRight to transpose first and deepest levels) into a new structure.
Using the toTree function from this answer (thanks to @b3m2a1 !):
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
toTree[Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]]
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
add a comment |
$begingroup$
This is a bit roundabout but very versatile. First convert the structure arr into a list of rules, then reassemble these rules (after proper modification using RotateRight to transpose first and deepest levels) into a new structure.
Using the toTree function from this answer (thanks to @b3m2a1 !):
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
toTree[Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]]
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
add a comment |
$begingroup$
This is a bit roundabout but very versatile. First convert the structure arr into a list of rules, then reassemble these rules (after proper modification using RotateRight to transpose first and deepest levels) into a new structure.
Using the toTree function from this answer (thanks to @b3m2a1 !):
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
toTree[Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]]
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
$endgroup$
This is a bit roundabout but very versatile. First convert the structure arr into a list of rules, then reassemble these rules (after proper modification using RotateRight to transpose first and deepest levels) into a new structure.
Using the toTree function from this answer (thanks to @b3m2a1 !):
arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;
toTree[Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]]]
a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b
answered Mar 30 at 21:29
RomanRoman
4,61511128
4,61511128
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');
);
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-do-i-transpose-the-first-and-deepest-levels-of-an-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');
);
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');
);
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');
);
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
Mar 29 at 19:41
$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
Mar 29 at 19:43
$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
Mar 29 at 19:52