Square array from linear array python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I would like to get a square matrix B
from a linear vector A
such that B = A * transpose(A)
. A
is a numpy array and np.shape(A)
returns (10,)
. I would like B
to be a (10,10)
array. I tried B = np.matmut(A, A[np.newaxis])
but I get an error :
shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)
python arrays numpy matrix vector
add a comment |
I would like to get a square matrix B
from a linear vector A
such that B = A * transpose(A)
. A
is a numpy array and np.shape(A)
returns (10,)
. I would like B
to be a (10,10)
array. I tried B = np.matmut(A, A[np.newaxis])
but I get an error :
shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)
python arrays numpy matrix vector
What I want is the equivalent of "B=A*ctranspose(A)" in matlab
– Mathieu Lecoq
Nov 23 '18 at 14:12
add a comment |
I would like to get a square matrix B
from a linear vector A
such that B = A * transpose(A)
. A
is a numpy array and np.shape(A)
returns (10,)
. I would like B
to be a (10,10)
array. I tried B = np.matmut(A, A[np.newaxis])
but I get an error :
shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)
python arrays numpy matrix vector
I would like to get a square matrix B
from a linear vector A
such that B = A * transpose(A)
. A
is a numpy array and np.shape(A)
returns (10,)
. I would like B
to be a (10,10)
array. I tried B = np.matmut(A, A[np.newaxis])
but I get an error :
shapes (10,) and (1,10) not aligned: 10 (dim 0) != 1 (dim 0)
python arrays numpy matrix vector
python arrays numpy matrix vector
edited Nov 23 '18 at 19:16
MarianD
4,46761432
4,46761432
asked Nov 23 '18 at 14:08
Mathieu LecoqMathieu Lecoq
83
83
What I want is the equivalent of "B=A*ctranspose(A)" in matlab
– Mathieu Lecoq
Nov 23 '18 at 14:12
add a comment |
What I want is the equivalent of "B=A*ctranspose(A)" in matlab
– Mathieu Lecoq
Nov 23 '18 at 14:12
What I want is the equivalent of "B=A*ctranspose(A)" in matlab
– Mathieu Lecoq
Nov 23 '18 at 14:12
What I want is the equivalent of "B=A*ctranspose(A)" in matlab
– Mathieu Lecoq
Nov 23 '18 at 14:12
add a comment |
3 Answers
3
active
oldest
votes
you can do this using outer
:
import numpy as np
vector = np.arange(10)
np.outer(vector, vector)
add a comment |
The solution is a little ugly, but it does what you need.
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = np.dot(vector[:,None],vector[None,:])
print(matrix)
You can also do the following:
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = vector*vector[:,None]
print(matrix)
The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.
add a comment |
Variation on outer product:
a = A.reshape(-1, 1) # make sure it's a column vector
B = a @ a.T
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53448202%2fsquare-array-from-linear-array-python%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
you can do this using outer
:
import numpy as np
vector = np.arange(10)
np.outer(vector, vector)
add a comment |
you can do this using outer
:
import numpy as np
vector = np.arange(10)
np.outer(vector, vector)
add a comment |
you can do this using outer
:
import numpy as np
vector = np.arange(10)
np.outer(vector, vector)
you can do this using outer
:
import numpy as np
vector = np.arange(10)
np.outer(vector, vector)
answered Nov 23 '18 at 14:20
jeremycgjeremycg
19.1k44257
19.1k44257
add a comment |
add a comment |
The solution is a little ugly, but it does what you need.
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = np.dot(vector[:,None],vector[None,:])
print(matrix)
You can also do the following:
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = vector*vector[:,None]
print(matrix)
The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.
add a comment |
The solution is a little ugly, but it does what you need.
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = np.dot(vector[:,None],vector[None,:])
print(matrix)
You can also do the following:
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = vector*vector[:,None]
print(matrix)
The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.
add a comment |
The solution is a little ugly, but it does what you need.
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = np.dot(vector[:,None],vector[None,:])
print(matrix)
You can also do the following:
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = vector*vector[:,None]
print(matrix)
The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.
The solution is a little ugly, but it does what you need.
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = np.dot(vector[:,None],vector[None,:])
print(matrix)
You can also do the following:
import numpy as np
vector = np.array([1,2,3,4,5,6,7,8,9,10],)
matrix = vector*vector[:,None]
print(matrix)
The issue comes from the fact that transposing a one dimensional array does not have the effect you might expect.
answered Nov 23 '18 at 14:19
Esteban QuirosEsteban Quiros
1015
1015
add a comment |
add a comment |
Variation on outer product:
a = A.reshape(-1, 1) # make sure it's a column vector
B = a @ a.T
add a comment |
Variation on outer product:
a = A.reshape(-1, 1) # make sure it's a column vector
B = a @ a.T
add a comment |
Variation on outer product:
a = A.reshape(-1, 1) # make sure it's a column vector
B = a @ a.T
Variation on outer product:
a = A.reshape(-1, 1) # make sure it's a column vector
B = a @ a.T
answered Nov 23 '18 at 15:32
deckarddeckard
31527
31527
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53448202%2fsquare-array-from-linear-array-python%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
What I want is the equivalent of "B=A*ctranspose(A)" in matlab
– Mathieu Lecoq
Nov 23 '18 at 14:12