How to convert diagonal matrix to rectangular matrixIs there a built in function to obtain the back diagonal of a matrix?How to select all elements above the main diagonal of matrix?Diagonal times dense matrix, high precisionHow to extract the list of all matrices from a Block Diagonal Matrix?Get rid of infinity in matrix elements (by separate definition of diagonal and off-diagonal elements)Summation over diagonal blocksHow to transform symmetric matrix to diagonal?Sum of elements in a diagonalReplace diagonal elements in sparse matrixConstructing a block diagonal matrix
How to convert diagonal matrix to rectangular matrix
What are the effects of abstaining from eating a certain flavor?
My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?
Why did Old English lose both thorn and eth?
Is homosexuality or bisexuality allowed for women?
Would denouncing cheaters from an exam make me less likely to receive penalties?
Matrices with shadows
Appropriate conduit for several data cables underground over 300' run
stuck in/at beta
Why do people prefer metropolitan areas, considering monsters and villains?
Wires do not connect in Circuitikz
My professor has told me he will be the corresponding author. Will it hurt my future career?
How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant
How many Jimmys can fit?
What is the average number of draws it takes before you can not draw any more cards from the Deck of Many Things?
Why won't the U.S. sign a peace treaty with North Korea?
What kind of Chinook helicopter/airplane hybrid is this?
Computer name naming convention for security
Find out what encryptor name my database is using
Is it okay to use open source code to do an interview task?
Category-theoretic treatment of diffs, patches and merging?
What does "spinning upon the shoals" mean?
Why am I getting unevenly-spread results when using $RANDOM?
Is it possible to complete a PhD in CS in 3 years?
How to convert diagonal matrix to rectangular matrix
Is there a built in function to obtain the back diagonal of a matrix?How to select all elements above the main diagonal of matrix?Diagonal times dense matrix, high precisionHow to extract the list of all matrices from a Block Diagonal Matrix?Get rid of infinity in matrix elements (by separate definition of diagonal and off-diagonal elements)Summation over diagonal blocksHow to transform symmetric matrix to diagonal?Sum of elements in a diagonalReplace diagonal elements in sparse matrixConstructing a block diagonal matrix
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Suppose you have the following diagonal matrix:
DiagonalMatrix[Hold/@a, b, c]//ReleaseHold
a, 0, 0, b, c
How can the above matrix be converted to the following rectangular one?
a, 0, 0, 0, b, c
Or another example:
b, c, 0, 0, a} -> b, c, 0, 0, 0, a
list-manipulation matrix
New contributor
$endgroup$
add a comment |
$begingroup$
Suppose you have the following diagonal matrix:
DiagonalMatrix[Hold/@a, b, c]//ReleaseHold
a, 0, 0, b, c
How can the above matrix be converted to the following rectangular one?
a, 0, 0, 0, b, c
Or another example:
b, c, 0, 0, a} -> b, c, 0, 0, 0, a
list-manipulation matrix
New contributor
$endgroup$
1
$begingroup$
Could you clarify what should happen for e.g. the matrixa, d, e, 0, 0, b, c
as in my answer? Whether my answer is correct or not hinges on this, because it gives a different answer than kglr.
$endgroup$
– C. E.
20 mins ago
add a comment |
$begingroup$
Suppose you have the following diagonal matrix:
DiagonalMatrix[Hold/@a, b, c]//ReleaseHold
a, 0, 0, b, c
How can the above matrix be converted to the following rectangular one?
a, 0, 0, 0, b, c
Or another example:
b, c, 0, 0, a} -> b, c, 0, 0, 0, a
list-manipulation matrix
New contributor
$endgroup$
Suppose you have the following diagonal matrix:
DiagonalMatrix[Hold/@a, b, c]//ReleaseHold
a, 0, 0, b, c
How can the above matrix be converted to the following rectangular one?
a, 0, 0, 0, b, c
Or another example:
b, c, 0, 0, a} -> b, c, 0, 0, 0, a
list-manipulation matrix
list-manipulation matrix
New contributor
New contributor
edited 2 mins ago
aleksander_si
New contributor
asked 8 hours ago
aleksander_sialeksander_si
284 bronze badges
284 bronze badges
New contributor
New contributor
1
$begingroup$
Could you clarify what should happen for e.g. the matrixa, d, e, 0, 0, b, c
as in my answer? Whether my answer is correct or not hinges on this, because it gives a different answer than kglr.
$endgroup$
– C. E.
20 mins ago
add a comment |
1
$begingroup$
Could you clarify what should happen for e.g. the matrixa, d, e, 0, 0, b, c
as in my answer? Whether my answer is correct or not hinges on this, because it gives a different answer than kglr.
$endgroup$
– C. E.
20 mins ago
1
1
$begingroup$
Could you clarify what should happen for e.g. the matrix
a, d, e, 0, 0, b, c
as in my answer? Whether my answer is correct or not hinges on this, because it gives a different answer than kglr.$endgroup$
– C. E.
20 mins ago
$begingroup$
Could you clarify what should happen for e.g. the matrix
a, d, e, 0, 0, b, c
as in my answer? Whether my answer is correct or not hinges on this, because it gives a different answer than kglr.$endgroup$
– C. E.
20 mins ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
kglr's solution fails if the number of elements on the diagonal in any of the preceding rows is larger than the number of elements on the diagonal of the last row.
For example:
PadRight[Flatten /@ a, d, e, 0, 0, b, c] // MatrixForm
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
PadRight[Flatten /@ m] // MatrixForm
Here is a different solution:
diag = Flatten[#] & /@ Diagonal[m];
ncols = Length@Flatten[diag];
offsets = Most@Prepend[Accumulate[Length /@ diag], 0];
row[values_, offset_, ncols_] := PadRight[ArrayPad[values, offset, 0], ncols]
matrix[diag_, offsets_, ncols_] := MapThread[row[#, #2, ncols] &, diag, offsets]
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
matrix[diag, offsets, ncols] // MatrixForm
$endgroup$
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
add a comment |
$begingroup$
PadRight[Flatten /@ a, 0, 0, b, c]
a, 0, 0, 0, b, c
$endgroup$
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
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
);
);
aleksander_si is a new contributor. Be nice, and check out our Code of Conduct.
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%2f201672%2fhow-to-convert-diagonal-matrix-to-rectangular-matrix%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
kglr's solution fails if the number of elements on the diagonal in any of the preceding rows is larger than the number of elements on the diagonal of the last row.
For example:
PadRight[Flatten /@ a, d, e, 0, 0, b, c] // MatrixForm
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
PadRight[Flatten /@ m] // MatrixForm
Here is a different solution:
diag = Flatten[#] & /@ Diagonal[m];
ncols = Length@Flatten[diag];
offsets = Most@Prepend[Accumulate[Length /@ diag], 0];
row[values_, offset_, ncols_] := PadRight[ArrayPad[values, offset, 0], ncols]
matrix[diag_, offsets_, ncols_] := MapThread[row[#, #2, ncols] &, diag, offsets]
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
matrix[diag, offsets, ncols] // MatrixForm
$endgroup$
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
add a comment |
$begingroup$
kglr's solution fails if the number of elements on the diagonal in any of the preceding rows is larger than the number of elements on the diagonal of the last row.
For example:
PadRight[Flatten /@ a, d, e, 0, 0, b, c] // MatrixForm
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
PadRight[Flatten /@ m] // MatrixForm
Here is a different solution:
diag = Flatten[#] & /@ Diagonal[m];
ncols = Length@Flatten[diag];
offsets = Most@Prepend[Accumulate[Length /@ diag], 0];
row[values_, offset_, ncols_] := PadRight[ArrayPad[values, offset, 0], ncols]
matrix[diag_, offsets_, ncols_] := MapThread[row[#, #2, ncols] &, diag, offsets]
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
matrix[diag, offsets, ncols] // MatrixForm
$endgroup$
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
add a comment |
$begingroup$
kglr's solution fails if the number of elements on the diagonal in any of the preceding rows is larger than the number of elements on the diagonal of the last row.
For example:
PadRight[Flatten /@ a, d, e, 0, 0, b, c] // MatrixForm
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
PadRight[Flatten /@ m] // MatrixForm
Here is a different solution:
diag = Flatten[#] & /@ Diagonal[m];
ncols = Length@Flatten[diag];
offsets = Most@Prepend[Accumulate[Length /@ diag], 0];
row[values_, offset_, ncols_] := PadRight[ArrayPad[values, offset, 0], ncols]
matrix[diag_, offsets_, ncols_] := MapThread[row[#, #2, ncols] &, diag, offsets]
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
matrix[diag, offsets, ncols] // MatrixForm
$endgroup$
kglr's solution fails if the number of elements on the diagonal in any of the preceding rows is larger than the number of elements on the diagonal of the last row.
For example:
PadRight[Flatten /@ a, d, e, 0, 0, b, c] // MatrixForm
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
PadRight[Flatten /@ m] // MatrixForm
Here is a different solution:
diag = Flatten[#] & /@ Diagonal[m];
ncols = Length@Flatten[diag];
offsets = Most@Prepend[Accumulate[Length /@ diag], 0];
row[values_, offset_, ncols_] := PadRight[ArrayPad[values, offset, 0], ncols]
matrix[diag_, offsets_, ncols_] := MapThread[row[#, #2, ncols] &, diag, offsets]
m = a, 0, 0, 0, b, c, d, e, f, 0, 0, 0, b, c
matrix[diag, offsets, ncols] // MatrixForm
edited 19 mins ago
answered 27 mins ago
C. E.C. E.
52.9k3 gold badges102 silver badges210 bronze badges
52.9k3 gold badges102 silver badges210 bronze badges
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
add a comment |
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
$begingroup$
Thank you very much for the effort, as per my test, this solution seems to fully solve the problem!
$endgroup$
– aleksander_si
5 mins ago
add a comment |
$begingroup$
PadRight[Flatten /@ a, 0, 0, b, c]
a, 0, 0, 0, b, c
$endgroup$
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
add a comment |
$begingroup$
PadRight[Flatten /@ a, 0, 0, b, c]
a, 0, 0, 0, b, c
$endgroup$
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
add a comment |
$begingroup$
PadRight[Flatten /@ a, 0, 0, b, c]
a, 0, 0, 0, b, c
$endgroup$
PadRight[Flatten /@ a, 0, 0, b, c]
a, 0, 0, 0, b, c
answered 7 hours ago
kglrkglr
203k10 gold badges232 silver badges463 bronze badges
203k10 gold badges232 silver badges463 bronze badges
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
add a comment |
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
$begingroup$
Yes, in a situation where the diagonal matrix is as per the below example, then this approach should not be applied: a,b,0,0,c}
$endgroup$
– aleksander_si
17 mins ago
add a comment |
aleksander_si is a new contributor. Be nice, and check out our Code of Conduct.
aleksander_si is a new contributor. Be nice, and check out our Code of Conduct.
aleksander_si is a new contributor. Be nice, and check out our Code of Conduct.
aleksander_si is a new contributor. Be nice, and check out our Code of Conduct.
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%2f201672%2fhow-to-convert-diagonal-matrix-to-rectangular-matrix%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
1
$begingroup$
Could you clarify what should happen for e.g. the matrix
a, d, e, 0, 0, b, c
as in my answer? Whether my answer is correct or not hinges on this, because it gives a different answer than kglr.$endgroup$
– C. E.
20 mins ago