Matrix in which rows are coordinates of points of a meshgrid
In this tutorial I find code like
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)
Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...
python arrays numpy mesh
add a comment |
In this tutorial I find code like
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)
Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...
python arrays numpy mesh
Numpy.repeat() for x and numpy.tile() for y coordinate.
– Ante
Nov 23 '18 at 8:10
@ante can you post it as an answer with code example?
– Basj
Nov 23 '18 at 9:43
@Bajs I posted answer with a code.
– Ante
Nov 23 '18 at 11:20
add a comment |
In this tutorial I find code like
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)
Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...
python arrays numpy mesh
In this tutorial I find code like
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
Y, X = np.meshgrid(y, x)
xy = np.vstack([X.ravel(), Y.ravel()]).T
print(xy)
Isn't there a shorter way to get a matrix where rows are coordinates of points of the meshgrid, than using meshgrid+vstack+ravel+transpose?
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
...
python arrays numpy mesh
python arrays numpy mesh
asked Nov 22 '18 at 8:47
BasjBasj
6,18632107233
6,18632107233
Numpy.repeat() for x and numpy.tile() for y coordinate.
– Ante
Nov 23 '18 at 8:10
@ante can you post it as an answer with code example?
– Basj
Nov 23 '18 at 9:43
@Bajs I posted answer with a code.
– Ante
Nov 23 '18 at 11:20
add a comment |
Numpy.repeat() for x and numpy.tile() for y coordinate.
– Ante
Nov 23 '18 at 8:10
@ante can you post it as an answer with code example?
– Basj
Nov 23 '18 at 9:43
@Bajs I posted answer with a code.
– Ante
Nov 23 '18 at 11:20
Numpy.repeat() for x and numpy.tile() for y coordinate.
– Ante
Nov 23 '18 at 8:10
Numpy.repeat() for x and numpy.tile() for y coordinate.
– Ante
Nov 23 '18 at 8:10
@ante can you post it as an answer with code example?
– Basj
Nov 23 '18 at 9:43
@ante can you post it as an answer with code example?
– Basj
Nov 23 '18 at 9:43
@Bajs I posted answer with a code.
– Ante
Nov 23 '18 at 11:20
@Bajs I posted answer with a code.
– Ante
Nov 23 '18 at 11:20
add a comment |
2 Answers
2
active
oldest
votes
You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:
from itertools import product
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]
add a comment |
Here is implementation with repeat and tile methods.
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.empty((len(x) * len(y), 2))
xy[:, 0] = np.repeat(x, len(y))
xy[:, 1] = np.tile(y, len(x))
print(xy)
I suppose meshgrid method uses same approach to fill it's result matrix.
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
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%2f53426968%2fmatrix-in-which-rows-are-coordinates-of-points-of-a-meshgrid%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
You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:
from itertools import product
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]
add a comment |
You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:
from itertools import product
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]
add a comment |
You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:
from itertools import product
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]
You can skip meshgrid and get what you want more directly by just taking the cartesian product of x and y:
from itertools import product
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.array(list(product(x,y)))
print(xy)
Output:
[[-5. -1. ]
[-5. -0.77777778]
[-5. -0.55555556]
[-5. -0.33333333]
[-5. -0.11111111]
[-5. 0.11111111]
[-5. 0.33333333]
[-5. 0.55555556]
[-5. 0.77777778]
[-5. 1. ]
[-4.47368421 -1. ]
[-4.47368421 -0.77777778]
[-4.47368421 -0.55555556]
[-4.47368421 -0.33333333]
...
]
answered Nov 22 '18 at 9:51
teltel
7,41621431
7,41621431
add a comment |
add a comment |
Here is implementation with repeat and tile methods.
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.empty((len(x) * len(y), 2))
xy[:, 0] = np.repeat(x, len(y))
xy[:, 1] = np.tile(y, len(x))
print(xy)
I suppose meshgrid method uses same approach to fill it's result matrix.
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
add a comment |
Here is implementation with repeat and tile methods.
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.empty((len(x) * len(y), 2))
xy[:, 0] = np.repeat(x, len(y))
xy[:, 1] = np.tile(y, len(x))
print(xy)
I suppose meshgrid method uses same approach to fill it's result matrix.
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
add a comment |
Here is implementation with repeat and tile methods.
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.empty((len(x) * len(y), 2))
xy[:, 0] = np.repeat(x, len(y))
xy[:, 1] = np.tile(y, len(x))
print(xy)
I suppose meshgrid method uses same approach to fill it's result matrix.
Here is implementation with repeat and tile methods.
import numpy as np
x = np.linspace(-5, 5, 20)
y = np.linspace(-1, 1, 10)
xy = np.empty((len(x) * len(y), 2))
xy[:, 0] = np.repeat(x, len(y))
xy[:, 1] = np.tile(y, len(x))
print(xy)
I suppose meshgrid method uses same approach to fill it's result matrix.
answered Nov 23 '18 at 10:28
AnteAnte
4,39451942
4,39451942
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
add a comment |
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
For future reference: docs.scipy.org/doc/numpy-1.15.0/reference/generated/…
– Basj
Nov 23 '18 at 12:54
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%2f53426968%2fmatrix-in-which-rows-are-coordinates-of-points-of-a-meshgrid%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

Numpy.repeat() for x and numpy.tile() for y coordinate.
– Ante
Nov 23 '18 at 8:10
@ante can you post it as an answer with code example?
– Basj
Nov 23 '18 at 9:43
@Bajs I posted answer with a code.
– Ante
Nov 23 '18 at 11:20