How to append a matrix element by elementHow to apply a permutation to a symmetric square matrix?How to find the distance of two lists?Partitioned matrix operationsPlotting Matrix versus MatrixMatrix expansion and reorganisationHow to remove empty row-column pairs from an asymmetric square matrixBuilding a matrix function one element at a timeScramble matrix under some conditioncreating a data framework using matrix tensorTake upper triangular part of matrix
Is there any evidence that the small canisters (10 liters) of 95% oxygen actually help with altitude sickness?
Apply brace expansion in "reverse order"
A player is constantly pestering me about rules, what do I do as a DM?
Does ultrasonic bath cleaning damage laboratory volumetric glassware calibration?
Fedora boot screen shows both Fedora logo and Lenovo logo. Why and How?
Should I hide continue button until tasks are completed?
STM Microcontroller burns every time
Could Sauron have read Tom Bombadil's mind if Tom had held the Palantir?
Did Karl Marx ever use any example that involved cotton and dollars to illustrate the way capital and surplus value were generated?
Why is the voltage measurement of this circuit different when the switch is on?
Should I include salary information on my CV?
Why does the numerical solution of an ODE move away from an unstable equilibrium?
What is the legal status of travelling with (unprescribed) methadone in your carry-on?
MH370 blackbox - is it still possible to retrieve data from it?
When is it ok to add filler to a story?
How can I deal with a coworker killed on the job
Is there any set of 2-6 notes that doesn't have a chord name?
How to append a matrix element by element
Analog is Obtuse!
How to perform Login Authentication at the client-side?
Impossible darts scores
How to split an equation over two lines?
How can I repair scratches on a painted French door?
Inverse-quotes-quine
How to append a matrix element by element
How to apply a permutation to a symmetric square matrix?How to find the distance of two lists?Partitioned matrix operationsPlotting Matrix versus MatrixMatrix expansion and reorganisationHow to remove empty row-column pairs from an asymmetric square matrixBuilding a matrix function one element at a timeScramble matrix under some conditioncreating a data framework using matrix tensorTake upper triangular part of matrix
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Consider if I have some matrices constructed as such:
A =
, ,
,
B =
a , b ,
c , d
C =
e , f ,
g , h
...
A
serves as a matrix of empty lists. I want to be able to append B
, C
, etc. to A
, to obtain a final matrix like
A =
a, e, ... , b, f, ... ,
c, g, ... , d, h, ...
How can I do this? My matrices are always square and of the same dimensions. An alternative which could also be useful is to Join
two matrices element by element, if the matrix elements of both are already lists.
list-manipulation matrix
$endgroup$
add a comment |
$begingroup$
Consider if I have some matrices constructed as such:
A =
, ,
,
B =
a , b ,
c , d
C =
e , f ,
g , h
...
A
serves as a matrix of empty lists. I want to be able to append B
, C
, etc. to A
, to obtain a final matrix like
A =
a, e, ... , b, f, ... ,
c, g, ... , d, h, ...
How can I do this? My matrices are always square and of the same dimensions. An alternative which could also be useful is to Join
two matrices element by element, if the matrix elements of both are already lists.
list-manipulation matrix
$endgroup$
$begingroup$
You wrote "My matrices are always square and of the same dimensions". That is not true. TryDimensions/@A,B
$endgroup$
– Fortsaint
6 hours ago
$begingroup$
Kai, withA
andB
as inputs what is the desired result:a, b, c, d
or, a, , b, , c, , d
?
$endgroup$
– kglr
2 hours ago
$begingroup$
@Fortsaint my meaning is that the matrices which are being appended are always the same dimension. MatrixA
is simply a matrix of empty lists to append to.
$endgroup$
– Kai
1 hour ago
$begingroup$
@kglr see the update
$endgroup$
– Kai
1 hour ago
$begingroup$
withA = , , , ;B = a, b, c, d;cc = e, f, g, h;
if you useTranspose[A, B, cc, 3, 1, 2]
you get, a, e, , b, f, , c, g, , d, h
. But your post says you wanta, e, b, f, c, g, d, h
, no?
$endgroup$
– kglr
27 mins ago
add a comment |
$begingroup$
Consider if I have some matrices constructed as such:
A =
, ,
,
B =
a , b ,
c , d
C =
e , f ,
g , h
...
A
serves as a matrix of empty lists. I want to be able to append B
, C
, etc. to A
, to obtain a final matrix like
A =
a, e, ... , b, f, ... ,
c, g, ... , d, h, ...
How can I do this? My matrices are always square and of the same dimensions. An alternative which could also be useful is to Join
two matrices element by element, if the matrix elements of both are already lists.
list-manipulation matrix
$endgroup$
Consider if I have some matrices constructed as such:
A =
, ,
,
B =
a , b ,
c , d
C =
e , f ,
g , h
...
A
serves as a matrix of empty lists. I want to be able to append B
, C
, etc. to A
, to obtain a final matrix like
A =
a, e, ... , b, f, ... ,
c, g, ... , d, h, ...
How can I do this? My matrices are always square and of the same dimensions. An alternative which could also be useful is to Join
two matrices element by element, if the matrix elements of both are already lists.
list-manipulation matrix
list-manipulation matrix
edited 1 hour ago
Kai
asked 8 hours ago
KaiKai
5941 silver badge9 bronze badges
5941 silver badge9 bronze badges
$begingroup$
You wrote "My matrices are always square and of the same dimensions". That is not true. TryDimensions/@A,B
$endgroup$
– Fortsaint
6 hours ago
$begingroup$
Kai, withA
andB
as inputs what is the desired result:a, b, c, d
or, a, , b, , c, , d
?
$endgroup$
– kglr
2 hours ago
$begingroup$
@Fortsaint my meaning is that the matrices which are being appended are always the same dimension. MatrixA
is simply a matrix of empty lists to append to.
$endgroup$
– Kai
1 hour ago
$begingroup$
@kglr see the update
$endgroup$
– Kai
1 hour ago
$begingroup$
withA = , , , ;B = a, b, c, d;cc = e, f, g, h;
if you useTranspose[A, B, cc, 3, 1, 2]
you get, a, e, , b, f, , c, g, , d, h
. But your post says you wanta, e, b, f, c, g, d, h
, no?
$endgroup$
– kglr
27 mins ago
add a comment |
$begingroup$
You wrote "My matrices are always square and of the same dimensions". That is not true. TryDimensions/@A,B
$endgroup$
– Fortsaint
6 hours ago
$begingroup$
Kai, withA
andB
as inputs what is the desired result:a, b, c, d
or, a, , b, , c, , d
?
$endgroup$
– kglr
2 hours ago
$begingroup$
@Fortsaint my meaning is that the matrices which are being appended are always the same dimension. MatrixA
is simply a matrix of empty lists to append to.
$endgroup$
– Kai
1 hour ago
$begingroup$
@kglr see the update
$endgroup$
– Kai
1 hour ago
$begingroup$
withA = , , , ;B = a, b, c, d;cc = e, f, g, h;
if you useTranspose[A, B, cc, 3, 1, 2]
you get, a, e, , b, f, , c, g, , d, h
. But your post says you wanta, e, b, f, c, g, d, h
, no?
$endgroup$
– kglr
27 mins ago
$begingroup$
You wrote "My matrices are always square and of the same dimensions". That is not true. Try
Dimensions/@A,B
$endgroup$
– Fortsaint
6 hours ago
$begingroup$
You wrote "My matrices are always square and of the same dimensions". That is not true. Try
Dimensions/@A,B
$endgroup$
– Fortsaint
6 hours ago
$begingroup$
Kai, with
A
and B
as inputs what is the desired result: a, b, c, d
or , a, , b, , c, , d
?$endgroup$
– kglr
2 hours ago
$begingroup$
Kai, with
A
and B
as inputs what is the desired result: a, b, c, d
or , a, , b, , c, , d
?$endgroup$
– kglr
2 hours ago
$begingroup$
@Fortsaint my meaning is that the matrices which are being appended are always the same dimension. Matrix
A
is simply a matrix of empty lists to append to.$endgroup$
– Kai
1 hour ago
$begingroup$
@Fortsaint my meaning is that the matrices which are being appended are always the same dimension. Matrix
A
is simply a matrix of empty lists to append to.$endgroup$
– Kai
1 hour ago
$begingroup$
@kglr see the update
$endgroup$
– Kai
1 hour ago
$begingroup$
@kglr see the update
$endgroup$
– Kai
1 hour ago
$begingroup$
with
A = , , , ;B = a, b, c, d;cc = e, f, g, h;
if you use Transpose[A, B, cc, 3, 1, 2]
you get , a, e, , b, f, , c, g, , d, h
. But your post says you want a, e, b, f, c, g, d, h
, no?$endgroup$
– kglr
27 mins ago
$begingroup$
with
A = , , , ;B = a, b, c, d;cc = e, f, g, h;
if you use Transpose[A, B, cc, 3, 1, 2]
you get , a, e, , b, f, , c, g, , d, h
. But your post says you want a, e, b, f, c, g, d, h
, no?$endgroup$
– kglr
27 mins ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Suppose you have 4 matrices:
SeedRandom[1]
a, b, c, d = RandomInteger[1, 4, 2, 2];
Then, you can use Transpose
to construct the desired matrix:
Transpose[a, b, c, d, 3, 1, 2]
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0
$endgroup$
$begingroup$
I thinkTranspose[a, b, c, d, 3, 1, 2]
may be enough.
$endgroup$
– Roman
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
To join nested arrays elementwise, use the third argument of Join
. For matrices, depth 1 (default) is rows, 2 is columns, and 3 is elements.
a = Array[&, 2,2];
b =
b1, b2,
b3, b4
;
listEach = Map[List, #, 2]&
appendEach[x_,y_] := Join[x, listEach[y], 3]
Print[appendEach[appendEach[a, b], b+5]]
(* b1, 5 + b1, b2, 5 + b2, b3, 5 + b3, b4, 5 + b4 *)
Try it online!
However, if you are appending many matrices it may be more efficient to create a list of matrices using Sow / Reap
, then Transpose
into a matrix of lists.
$endgroup$
$begingroup$
What is the function of the&
in the first line?
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
MapThread
Flatten
at Level
2:
MapThread[Flatten[##] &, A, B, 2]
a, b, c, d
$endgroup$
add a comment |
Your Answer
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%2f200811%2fhow-to-append-a-matrix-element-by-element%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$
Suppose you have 4 matrices:
SeedRandom[1]
a, b, c, d = RandomInteger[1, 4, 2, 2];
Then, you can use Transpose
to construct the desired matrix:
Transpose[a, b, c, d, 3, 1, 2]
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0
$endgroup$
$begingroup$
I thinkTranspose[a, b, c, d, 3, 1, 2]
may be enough.
$endgroup$
– Roman
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
Suppose you have 4 matrices:
SeedRandom[1]
a, b, c, d = RandomInteger[1, 4, 2, 2];
Then, you can use Transpose
to construct the desired matrix:
Transpose[a, b, c, d, 3, 1, 2]
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0
$endgroup$
$begingroup$
I thinkTranspose[a, b, c, d, 3, 1, 2]
may be enough.
$endgroup$
– Roman
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
Suppose you have 4 matrices:
SeedRandom[1]
a, b, c, d = RandomInteger[1, 4, 2, 2];
Then, you can use Transpose
to construct the desired matrix:
Transpose[a, b, c, d, 3, 1, 2]
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0
$endgroup$
Suppose you have 4 matrices:
SeedRandom[1]
a, b, c, d = RandomInteger[1, 4, 2, 2];
Then, you can use Transpose
to construct the desired matrix:
Transpose[a, b, c, d, 3, 1, 2]
1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0
edited 4 hours ago
answered 6 hours ago
Carl WollCarl Woll
85.1k3 gold badges109 silver badges220 bronze badges
85.1k3 gold badges109 silver badges220 bronze badges
$begingroup$
I thinkTranspose[a, b, c, d, 3, 1, 2]
may be enough.
$endgroup$
– Roman
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
I thinkTranspose[a, b, c, d, 3, 1, 2]
may be enough.
$endgroup$
– Roman
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
$begingroup$
I think
Transpose[a, b, c, d, 3, 1, 2]
may be enough.$endgroup$
– Roman
4 hours ago
$begingroup$
I think
Transpose[a, b, c, d, 3, 1, 2]
may be enough.$endgroup$
– Roman
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
@Roman Thanks, much simpler!
$endgroup$
– Carl Woll
4 hours ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
$begingroup$
I'm still wrapping my head around the second argument of transpose, but this looks like the simplest way for me to achieve what I want.
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
To join nested arrays elementwise, use the third argument of Join
. For matrices, depth 1 (default) is rows, 2 is columns, and 3 is elements.
a = Array[&, 2,2];
b =
b1, b2,
b3, b4
;
listEach = Map[List, #, 2]&
appendEach[x_,y_] := Join[x, listEach[y], 3]
Print[appendEach[appendEach[a, b], b+5]]
(* b1, 5 + b1, b2, 5 + b2, b3, 5 + b3, b4, 5 + b4 *)
Try it online!
However, if you are appending many matrices it may be more efficient to create a list of matrices using Sow / Reap
, then Transpose
into a matrix of lists.
$endgroup$
$begingroup$
What is the function of the&
in the first line?
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
To join nested arrays elementwise, use the third argument of Join
. For matrices, depth 1 (default) is rows, 2 is columns, and 3 is elements.
a = Array[&, 2,2];
b =
b1, b2,
b3, b4
;
listEach = Map[List, #, 2]&
appendEach[x_,y_] := Join[x, listEach[y], 3]
Print[appendEach[appendEach[a, b], b+5]]
(* b1, 5 + b1, b2, 5 + b2, b3, 5 + b3, b4, 5 + b4 *)
Try it online!
However, if you are appending many matrices it may be more efficient to create a list of matrices using Sow / Reap
, then Transpose
into a matrix of lists.
$endgroup$
$begingroup$
What is the function of the&
in the first line?
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
To join nested arrays elementwise, use the third argument of Join
. For matrices, depth 1 (default) is rows, 2 is columns, and 3 is elements.
a = Array[&, 2,2];
b =
b1, b2,
b3, b4
;
listEach = Map[List, #, 2]&
appendEach[x_,y_] := Join[x, listEach[y], 3]
Print[appendEach[appendEach[a, b], b+5]]
(* b1, 5 + b1, b2, 5 + b2, b3, 5 + b3, b4, 5 + b4 *)
Try it online!
However, if you are appending many matrices it may be more efficient to create a list of matrices using Sow / Reap
, then Transpose
into a matrix of lists.
$endgroup$
To join nested arrays elementwise, use the third argument of Join
. For matrices, depth 1 (default) is rows, 2 is columns, and 3 is elements.
a = Array[&, 2,2];
b =
b1, b2,
b3, b4
;
listEach = Map[List, #, 2]&
appendEach[x_,y_] := Join[x, listEach[y], 3]
Print[appendEach[appendEach[a, b], b+5]]
(* b1, 5 + b1, b2, 5 + b2, b3, 5 + b3, b4, 5 + b4 *)
Try it online!
However, if you are appending many matrices it may be more efficient to create a list of matrices using Sow / Reap
, then Transpose
into a matrix of lists.
answered 7 hours ago
lirtosiastlirtosiast
1785 bronze badges
1785 bronze badges
$begingroup$
What is the function of the&
in the first line?
$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
What is the function of the&
in the first line?
$endgroup$
– Kai
1 hour ago
$begingroup$
What is the function of the
&
in the first line?$endgroup$
– Kai
1 hour ago
$begingroup$
What is the function of the
&
in the first line?$endgroup$
– Kai
1 hour ago
add a comment |
$begingroup$
MapThread
Flatten
at Level
2:
MapThread[Flatten[##] &, A, B, 2]
a, b, c, d
$endgroup$
add a comment |
$begingroup$
MapThread
Flatten
at Level
2:
MapThread[Flatten[##] &, A, B, 2]
a, b, c, d
$endgroup$
add a comment |
$begingroup$
MapThread
Flatten
at Level
2:
MapThread[Flatten[##] &, A, B, 2]
a, b, c, d
$endgroup$
MapThread
Flatten
at Level
2:
MapThread[Flatten[##] &, A, B, 2]
a, b, c, d
edited 6 hours ago
answered 6 hours ago
kglrkglr
200k10 gold badges229 silver badges455 bronze badges
200k10 gold badges229 silver badges455 bronze badges
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%2f200811%2fhow-to-append-a-matrix-element-by-element%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$
You wrote "My matrices are always square and of the same dimensions". That is not true. Try
Dimensions/@A,B
$endgroup$
– Fortsaint
6 hours ago
$begingroup$
Kai, with
A
andB
as inputs what is the desired result:a, b, c, d
or, a, , b, , c, , d
?$endgroup$
– kglr
2 hours ago
$begingroup$
@Fortsaint my meaning is that the matrices which are being appended are always the same dimension. Matrix
A
is simply a matrix of empty lists to append to.$endgroup$
– Kai
1 hour ago
$begingroup$
@kglr see the update
$endgroup$
– Kai
1 hour ago
$begingroup$
with
A = , , , ;B = a, b, c, d;cc = e, f, g, h;
if you useTranspose[A, B, cc, 3, 1, 2]
you get, a, e, , b, f, , c, g, , d, h
. But your post says you wanta, e, b, f, c, g, d, h
, no?$endgroup$
– kglr
27 mins ago