Creating a diagonal matrix from from a vector
$begingroup$
I try to create a function that create a diagonal matrix in which the diagonal are:
$qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$
Here is my attempt:
nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];
For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix
does not work as I expect.
Any idea as to where the mistake is in the function I created?
functions matrix table
$endgroup$
add a comment |
$begingroup$
I try to create a function that create a diagonal matrix in which the diagonal are:
$qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$
Here is my attempt:
nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];
For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix
does not work as I expect.
Any idea as to where the mistake is in the function I created?
functions matrix table
$endgroup$
add a comment |
$begingroup$
I try to create a function that create a diagonal matrix in which the diagonal are:
$qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$
Here is my attempt:
nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];
For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix
does not work as I expect.
Any idea as to where the mistake is in the function I created?
functions matrix table
$endgroup$
I try to create a function that create a diagonal matrix in which the diagonal are:
$qquad 1,a,a^2, cdots,a^n,1,a,a^2, cdots,a^n$
Here is my attempt:
nth[n_] := DiagonalMatrix[Table[a^{Mod[k, n]}, {k, 0, 2 n - 1}]];
For some reason and I do not know wh,y the diagonal matrix is just one vector. So it seems DiagonalMatrix
does not work as I expect.
Any idea as to where the mistake is in the function I created?
functions matrix table
functions matrix table
edited 39 mins ago
m_goldberg
85.9k872196
85.9k872196
asked 6 hours ago
henryhenry
1346
1346
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Remove the braces around Mod
:
ClearAll[nth]
nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]
nth[5] // MatrixForm // TeXForm
$left(
begin{array}{cccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
end{array}
right)$
Alternatively, you can use SparseArray
:
ClearAll[nth2]
nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
nth2[5] // MatrixForm // TeXForm
same result
or use Band
in combination with SparseArray
:
ClearAll[nth3]
nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]
nth2[5] // MatrixForm // TeXForm
same result
$endgroup$
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry,SparseArray
+Band
is likely to be the faster thanDiagonalMatrix
.
$endgroup$
– kglr
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry, you should useMatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
$endgroup$
– kglr
5 hours ago
|
show 1 more 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%2f191217%2fcreating-a-diagonal-matrix-from-from-a-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Remove the braces around Mod
:
ClearAll[nth]
nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]
nth[5] // MatrixForm // TeXForm
$left(
begin{array}{cccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
end{array}
right)$
Alternatively, you can use SparseArray
:
ClearAll[nth2]
nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
nth2[5] // MatrixForm // TeXForm
same result
or use Band
in combination with SparseArray
:
ClearAll[nth3]
nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]
nth2[5] // MatrixForm // TeXForm
same result
$endgroup$
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry,SparseArray
+Band
is likely to be the faster thanDiagonalMatrix
.
$endgroup$
– kglr
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry, you should useMatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
$endgroup$
– kglr
5 hours ago
|
show 1 more comment
$begingroup$
Remove the braces around Mod
:
ClearAll[nth]
nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]
nth[5] // MatrixForm // TeXForm
$left(
begin{array}{cccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
end{array}
right)$
Alternatively, you can use SparseArray
:
ClearAll[nth2]
nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
nth2[5] // MatrixForm // TeXForm
same result
or use Band
in combination with SparseArray
:
ClearAll[nth3]
nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]
nth2[5] // MatrixForm // TeXForm
same result
$endgroup$
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry,SparseArray
+Band
is likely to be the faster thanDiagonalMatrix
.
$endgroup$
– kglr
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry, you should useMatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
$endgroup$
– kglr
5 hours ago
|
show 1 more comment
$begingroup$
Remove the braces around Mod
:
ClearAll[nth]
nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]
nth[5] // MatrixForm // TeXForm
$left(
begin{array}{cccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
end{array}
right)$
Alternatively, you can use SparseArray
:
ClearAll[nth2]
nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
nth2[5] // MatrixForm // TeXForm
same result
or use Band
in combination with SparseArray
:
ClearAll[nth3]
nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]
nth2[5] // MatrixForm // TeXForm
same result
$endgroup$
Remove the braces around Mod
:
ClearAll[nth]
nth[n_] := DiagonalMatrix@Table[a^Mod[k, n], {k, 0, 2 n - 1}]
nth[5] // MatrixForm // TeXForm
$left(
begin{array}{cccccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & a & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & a^2 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & a^3 & 0 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & a^4 & 0 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & a & 0 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & a^2 & 0 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^3 & 0 \
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & a^4 \
end{array}
right)$
Alternatively, you can use SparseArray
:
ClearAll[nth2]
nth2[n_] := SparseArray[{k_, k_} :> a^Mod[k, n], {2 n - 1, 2 n - 1}]
nth2[5] // MatrixForm // TeXForm
same result
or use Band
in combination with SparseArray
:
ClearAll[nth3]
nth3[n_] := SparseArray[Band[{1, 1}] -> Table[a^Mod[k, n], {k, 0, 2 n - 1}]]
nth2[5] // MatrixForm // TeXForm
same result
edited 6 hours ago
answered 6 hours ago
kglrkglr
183k10201416
183k10201416
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry,SparseArray
+Band
is likely to be the faster thanDiagonalMatrix
.
$endgroup$
– kglr
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry, you should useMatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
$endgroup$
– kglr
5 hours ago
|
show 1 more comment
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry,SparseArray
+Band
is likely to be the faster thanDiagonalMatrix
.
$endgroup$
– kglr
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
1
$begingroup$
@henry, you should useMatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )
$endgroup$
– kglr
5 hours ago
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
thanks.. it work perfectly.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
$begingroup$
which one more practical? I mean later i need to compute power of modified verson of this matrix. so it will be much more computation.
$endgroup$
– henry
6 hours ago
1
1
$begingroup$
@henry,
SparseArray
+Band
is likely to be the faster than DiagonalMatrix
.$endgroup$
– kglr
6 hours ago
$begingroup$
@henry,
SparseArray
+Band
is likely to be the faster than DiagonalMatrix
.$endgroup$
– kglr
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
$begingroup$
final question. If i use MatrixForm as a part of the function. does have any effect of efficiency of the computation?
$endgroup$
– henry
6 hours ago
1
1
$begingroup$
@henry, you should use
MatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )$endgroup$
– kglr
5 hours ago
$begingroup$
@henry, you should use
MatrixForm
is only for displaying (MatrixForm acts as a "wrapper", which affects printing, but not evaluation. )$endgroup$
– kglr
5 hours ago
|
show 1 more 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%2f191217%2fcreating-a-diagonal-matrix-from-from-a-vector%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