How to efficiently unroll a matrix by value with numpy?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I sort a dictionary by value?How do I determine whether an array contains a particular value in Java?How do I list all files of a directory?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
Which country benefited the most from UN Security Council vetoes?
How old can references or sources in a thesis be?
Does detail obscure or enhance action?
tikz convert color string to hex value
Fully-Firstable Anagram Sets
Character reincarnated...as a snail
Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)
Maximum likelihood parameters deviate from posterior distributions
Can I make popcorn with any corn?
Did Shadowfax go to Valinor?
Arrow those variables!
Can you really stack all of this on an Opportunity Attack?
Cross compiling for RPi - error while loading shared libraries
Add text to same line using sed
Why can't I see bouncing of a switch on an oscilloscope?
High voltage LED indicator 40-1000 VDC without additional power supply
What defenses are there against being summoned by the Gate spell?
What's the output of a record needle playing an out-of-speed record
How to efficiently unroll a matrix by value with numpy?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Does an object always see its latest internal state irrespective of thread?
Has there ever been an airliner design involving reducing generator load by installing solar panels?
How to determine what difficulty is right for the game?
How do I deal with an unproductive colleague in a small company?
How to efficiently unroll a matrix by value with numpy?
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I sort a dictionary by value?How do I determine whether an array contains a particular value in Java?How do I list all files of a directory?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a matrix M
with values 0 through N
within it. I'd like to unroll this matrix to create a new matrix A
where each submatrix A[i, :, :]
represents whether or not M == i.
The solution below uses a loop.
# Example Setup
import numpy as np
np.random.seed(0)
N = 5
M = np.random.randint(0, N, size=(5,5))
# Solution with Loop
A = np.zeros((N, M.shape[0], M.shape[1]))
for i in range(N):
A[i, :, :] = M == i
This yields:
M
array([[4, 0, 3, 3, 3],
[1, 3, 2, 4, 0],
[0, 4, 2, 1, 0],
[1, 1, 0, 1, 4],
[3, 0, 3, 0, 2]])
M.shape
# (5, 5)
A
array([[[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0]],
...
[[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0]]])
A.shape
# (5, 5, 5)
Is there a faster way, or a way to do it in a single numpy operation?
python arrays numpy
|
show 2 more comments
I have a matrix M
with values 0 through N
within it. I'd like to unroll this matrix to create a new matrix A
where each submatrix A[i, :, :]
represents whether or not M == i.
The solution below uses a loop.
# Example Setup
import numpy as np
np.random.seed(0)
N = 5
M = np.random.randint(0, N, size=(5,5))
# Solution with Loop
A = np.zeros((N, M.shape[0], M.shape[1]))
for i in range(N):
A[i, :, :] = M == i
This yields:
M
array([[4, 0, 3, 3, 3],
[1, 3, 2, 4, 0],
[0, 4, 2, 1, 0],
[1, 1, 0, 1, 4],
[3, 0, 3, 0, 2]])
M.shape
# (5, 5)
A
array([[[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0]],
...
[[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0]]])
A.shape
# (5, 5, 5)
Is there a faster way, or a way to do it in a single numpy operation?
python arrays numpy
It would be better if you explain it in detail.
– Marios Nikolaou
7 hours ago
2
@MariosNikolaou just copy/paste his code andprint(M)
;print(A)
...I edited it for you though
– Reedinationer
7 hours ago
@Reedinationer i did it.
– Marios Nikolaou
7 hours ago
I would not recommend pasting output for this code as the input is randomised without a seed.
– coldspeed
7 hours ago
1
i == M
compare int with array 5x5 ? and then save it in A?
– Marios Nikolaou
7 hours ago
|
show 2 more comments
I have a matrix M
with values 0 through N
within it. I'd like to unroll this matrix to create a new matrix A
where each submatrix A[i, :, :]
represents whether or not M == i.
The solution below uses a loop.
# Example Setup
import numpy as np
np.random.seed(0)
N = 5
M = np.random.randint(0, N, size=(5,5))
# Solution with Loop
A = np.zeros((N, M.shape[0], M.shape[1]))
for i in range(N):
A[i, :, :] = M == i
This yields:
M
array([[4, 0, 3, 3, 3],
[1, 3, 2, 4, 0],
[0, 4, 2, 1, 0],
[1, 1, 0, 1, 4],
[3, 0, 3, 0, 2]])
M.shape
# (5, 5)
A
array([[[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0]],
...
[[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0]]])
A.shape
# (5, 5, 5)
Is there a faster way, or a way to do it in a single numpy operation?
python arrays numpy
I have a matrix M
with values 0 through N
within it. I'd like to unroll this matrix to create a new matrix A
where each submatrix A[i, :, :]
represents whether or not M == i.
The solution below uses a loop.
# Example Setup
import numpy as np
np.random.seed(0)
N = 5
M = np.random.randint(0, N, size=(5,5))
# Solution with Loop
A = np.zeros((N, M.shape[0], M.shape[1]))
for i in range(N):
A[i, :, :] = M == i
This yields:
M
array([[4, 0, 3, 3, 3],
[1, 3, 2, 4, 0],
[0, 4, 2, 1, 0],
[1, 1, 0, 1, 4],
[3, 0, 3, 0, 2]])
M.shape
# (5, 5)
A
array([[[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 0]],
...
[[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 0]]])
A.shape
# (5, 5, 5)
Is there a faster way, or a way to do it in a single numpy operation?
python arrays numpy
python arrays numpy
edited 7 hours ago
coldspeed
140k24156241
140k24156241
asked 7 hours ago
seveibarseveibar
1,29211225
1,29211225
It would be better if you explain it in detail.
– Marios Nikolaou
7 hours ago
2
@MariosNikolaou just copy/paste his code andprint(M)
;print(A)
...I edited it for you though
– Reedinationer
7 hours ago
@Reedinationer i did it.
– Marios Nikolaou
7 hours ago
I would not recommend pasting output for this code as the input is randomised without a seed.
– coldspeed
7 hours ago
1
i == M
compare int with array 5x5 ? and then save it in A?
– Marios Nikolaou
7 hours ago
|
show 2 more comments
It would be better if you explain it in detail.
– Marios Nikolaou
7 hours ago
2
@MariosNikolaou just copy/paste his code andprint(M)
;print(A)
...I edited it for you though
– Reedinationer
7 hours ago
@Reedinationer i did it.
– Marios Nikolaou
7 hours ago
I would not recommend pasting output for this code as the input is randomised without a seed.
– coldspeed
7 hours ago
1
i == M
compare int with array 5x5 ? and then save it in A?
– Marios Nikolaou
7 hours ago
It would be better if you explain it in detail.
– Marios Nikolaou
7 hours ago
It would be better if you explain it in detail.
– Marios Nikolaou
7 hours ago
2
2
@MariosNikolaou just copy/paste his code and
print(M)
;print(A)
...I edited it for you though– Reedinationer
7 hours ago
@MariosNikolaou just copy/paste his code and
print(M)
;print(A)
...I edited it for you though– Reedinationer
7 hours ago
@Reedinationer i did it.
– Marios Nikolaou
7 hours ago
@Reedinationer i did it.
– Marios Nikolaou
7 hours ago
I would not recommend pasting output for this code as the input is randomised without a seed.
– coldspeed
7 hours ago
I would not recommend pasting output for this code as the input is randomised without a seed.
– coldspeed
7 hours ago
1
1
i == M
compare int with array 5x5 ? and then save it in A?– Marios Nikolaou
7 hours ago
i == M
compare int with array 5x5 ? and then save it in A?– Marios Nikolaou
7 hours ago
|
show 2 more comments
3 Answers
3
active
oldest
votes
You can make use of some broadcasting here:
P = np.arange(N)
Y = np.broadcast_to(P[:, None], M.shape)
T = np.equal(M, Y[:, None]).astype(int)
Alternative using indices
:
X, Y = np.indices(M.shape)
Z = np.equal(M, X[:, None]).astype(int)
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
add a comment |
Broadcasted comparison is your friend:
B = (M[None, :] == np.arange(N)[:, None, None]).view(np.int8)
np.array_equal(A, B)
# True
The idea is to expand the dimensions in such a way that the comparison can be broadcasted in the manner desired.
As pointed out by @Alex Riley in the comments, you can use np.equal.outer
to avoid having to do the indexing stuff yourself,
B = np.equal.outer(np.arange(N), M).view(np.int8)
np.array_equal(A, B)
# True
1
Good answer - just to point out there's a superfluous newaxis in your indexing forM
(resulting in a 4D array). You could useM[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to usenp.equal.outer(np.arange(N), M).view(np.int8)
.
– Alex Riley
6 hours ago
@AlexRiley Thanks for that! And theouter
solution is quite neat.
– coldspeed
6 hours ago
add a comment |
You can index into the identity matrix like so
A = np.identity(N, int)[:, M]
or so
A = np.identity(N, int)[M.T].T
Or use the new (v1.15.0) put_along_axis
A = np.zeros((N,5,5), int)
np.put_along_axis(A, M[None], 1, 0)
Note if N is much larger than 5 then creating an NxN identity matrix may be considered wasteful. We can mitigate this using stride tricks:
def read_only_identity(N, dtype=float):
z = np.zeros(2*N-1, dtype)
s, = z.strides
z[N-1] = 1
return np.lib.stride_tricks.as_strided(z[N-1:], (N, N), (-s, s))
1
This is great, really interesting answer.
– user3483203
5 hours ago
1
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
1
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2f55543949%2fhow-to-efficiently-unroll-a-matrix-by-value-with-numpy%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 make use of some broadcasting here:
P = np.arange(N)
Y = np.broadcast_to(P[:, None], M.shape)
T = np.equal(M, Y[:, None]).astype(int)
Alternative using indices
:
X, Y = np.indices(M.shape)
Z = np.equal(M, X[:, None]).astype(int)
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
add a comment |
You can make use of some broadcasting here:
P = np.arange(N)
Y = np.broadcast_to(P[:, None], M.shape)
T = np.equal(M, Y[:, None]).astype(int)
Alternative using indices
:
X, Y = np.indices(M.shape)
Z = np.equal(M, X[:, None]).astype(int)
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
add a comment |
You can make use of some broadcasting here:
P = np.arange(N)
Y = np.broadcast_to(P[:, None], M.shape)
T = np.equal(M, Y[:, None]).astype(int)
Alternative using indices
:
X, Y = np.indices(M.shape)
Z = np.equal(M, X[:, None]).astype(int)
You can make use of some broadcasting here:
P = np.arange(N)
Y = np.broadcast_to(P[:, None], M.shape)
T = np.equal(M, Y[:, None]).astype(int)
Alternative using indices
:
X, Y = np.indices(M.shape)
Z = np.equal(M, X[:, None]).astype(int)
edited 6 hours ago
answered 7 hours ago
user3483203user3483203
31.8k82857
31.8k82857
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
add a comment |
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
This answer was really helpful towards my understanding of broadcasting, thank you!
– seveibar
4 hours ago
add a comment |
Broadcasted comparison is your friend:
B = (M[None, :] == np.arange(N)[:, None, None]).view(np.int8)
np.array_equal(A, B)
# True
The idea is to expand the dimensions in such a way that the comparison can be broadcasted in the manner desired.
As pointed out by @Alex Riley in the comments, you can use np.equal.outer
to avoid having to do the indexing stuff yourself,
B = np.equal.outer(np.arange(N), M).view(np.int8)
np.array_equal(A, B)
# True
1
Good answer - just to point out there's a superfluous newaxis in your indexing forM
(resulting in a 4D array). You could useM[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to usenp.equal.outer(np.arange(N), M).view(np.int8)
.
– Alex Riley
6 hours ago
@AlexRiley Thanks for that! And theouter
solution is quite neat.
– coldspeed
6 hours ago
add a comment |
Broadcasted comparison is your friend:
B = (M[None, :] == np.arange(N)[:, None, None]).view(np.int8)
np.array_equal(A, B)
# True
The idea is to expand the dimensions in such a way that the comparison can be broadcasted in the manner desired.
As pointed out by @Alex Riley in the comments, you can use np.equal.outer
to avoid having to do the indexing stuff yourself,
B = np.equal.outer(np.arange(N), M).view(np.int8)
np.array_equal(A, B)
# True
1
Good answer - just to point out there's a superfluous newaxis in your indexing forM
(resulting in a 4D array). You could useM[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to usenp.equal.outer(np.arange(N), M).view(np.int8)
.
– Alex Riley
6 hours ago
@AlexRiley Thanks for that! And theouter
solution is quite neat.
– coldspeed
6 hours ago
add a comment |
Broadcasted comparison is your friend:
B = (M[None, :] == np.arange(N)[:, None, None]).view(np.int8)
np.array_equal(A, B)
# True
The idea is to expand the dimensions in such a way that the comparison can be broadcasted in the manner desired.
As pointed out by @Alex Riley in the comments, you can use np.equal.outer
to avoid having to do the indexing stuff yourself,
B = np.equal.outer(np.arange(N), M).view(np.int8)
np.array_equal(A, B)
# True
Broadcasted comparison is your friend:
B = (M[None, :] == np.arange(N)[:, None, None]).view(np.int8)
np.array_equal(A, B)
# True
The idea is to expand the dimensions in such a way that the comparison can be broadcasted in the manner desired.
As pointed out by @Alex Riley in the comments, you can use np.equal.outer
to avoid having to do the indexing stuff yourself,
B = np.equal.outer(np.arange(N), M).view(np.int8)
np.array_equal(A, B)
# True
edited 6 hours ago
answered 7 hours ago
coldspeedcoldspeed
140k24156241
140k24156241
1
Good answer - just to point out there's a superfluous newaxis in your indexing forM
(resulting in a 4D array). You could useM[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to usenp.equal.outer(np.arange(N), M).view(np.int8)
.
– Alex Riley
6 hours ago
@AlexRiley Thanks for that! And theouter
solution is quite neat.
– coldspeed
6 hours ago
add a comment |
1
Good answer - just to point out there's a superfluous newaxis in your indexing forM
(resulting in a 4D array). You could useM[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to usenp.equal.outer(np.arange(N), M).view(np.int8)
.
– Alex Riley
6 hours ago
@AlexRiley Thanks for that! And theouter
solution is quite neat.
– coldspeed
6 hours ago
1
1
Good answer - just to point out there's a superfluous newaxis in your indexing for
M
(resulting in a 4D array). You could use M[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to use np.equal.outer(np.arange(N), M).view(np.int8)
.– Alex Riley
6 hours ago
Good answer - just to point out there's a superfluous newaxis in your indexing for
M
(resulting in a 4D array). You could use M[None, :]
instead to get the 3D array. An alternative to avoid fiddly indexing is to use np.equal.outer(np.arange(N), M).view(np.int8)
.– Alex Riley
6 hours ago
@AlexRiley Thanks for that! And the
outer
solution is quite neat.– coldspeed
6 hours ago
@AlexRiley Thanks for that! And the
outer
solution is quite neat.– coldspeed
6 hours ago
add a comment |
You can index into the identity matrix like so
A = np.identity(N, int)[:, M]
or so
A = np.identity(N, int)[M.T].T
Or use the new (v1.15.0) put_along_axis
A = np.zeros((N,5,5), int)
np.put_along_axis(A, M[None], 1, 0)
Note if N is much larger than 5 then creating an NxN identity matrix may be considered wasteful. We can mitigate this using stride tricks:
def read_only_identity(N, dtype=float):
z = np.zeros(2*N-1, dtype)
s, = z.strides
z[N-1] = 1
return np.lib.stride_tricks.as_strided(z[N-1:], (N, N), (-s, s))
1
This is great, really interesting answer.
– user3483203
5 hours ago
1
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
1
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
add a comment |
You can index into the identity matrix like so
A = np.identity(N, int)[:, M]
or so
A = np.identity(N, int)[M.T].T
Or use the new (v1.15.0) put_along_axis
A = np.zeros((N,5,5), int)
np.put_along_axis(A, M[None], 1, 0)
Note if N is much larger than 5 then creating an NxN identity matrix may be considered wasteful. We can mitigate this using stride tricks:
def read_only_identity(N, dtype=float):
z = np.zeros(2*N-1, dtype)
s, = z.strides
z[N-1] = 1
return np.lib.stride_tricks.as_strided(z[N-1:], (N, N), (-s, s))
1
This is great, really interesting answer.
– user3483203
5 hours ago
1
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
1
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
add a comment |
You can index into the identity matrix like so
A = np.identity(N, int)[:, M]
or so
A = np.identity(N, int)[M.T].T
Or use the new (v1.15.0) put_along_axis
A = np.zeros((N,5,5), int)
np.put_along_axis(A, M[None], 1, 0)
Note if N is much larger than 5 then creating an NxN identity matrix may be considered wasteful. We can mitigate this using stride tricks:
def read_only_identity(N, dtype=float):
z = np.zeros(2*N-1, dtype)
s, = z.strides
z[N-1] = 1
return np.lib.stride_tricks.as_strided(z[N-1:], (N, N), (-s, s))
You can index into the identity matrix like so
A = np.identity(N, int)[:, M]
or so
A = np.identity(N, int)[M.T].T
Or use the new (v1.15.0) put_along_axis
A = np.zeros((N,5,5), int)
np.put_along_axis(A, M[None], 1, 0)
Note if N is much larger than 5 then creating an NxN identity matrix may be considered wasteful. We can mitigate this using stride tricks:
def read_only_identity(N, dtype=float):
z = np.zeros(2*N-1, dtype)
s, = z.strides
z[N-1] = 1
return np.lib.stride_tricks.as_strided(z[N-1:], (N, N), (-s, s))
edited 4 hours ago
answered 5 hours ago
Paul PanzerPaul Panzer
31.5k21845
31.5k21845
1
This is great, really interesting answer.
– user3483203
5 hours ago
1
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
1
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
add a comment |
1
This is great, really interesting answer.
– user3483203
5 hours ago
1
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
1
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
1
1
This is great, really interesting answer.
– user3483203
5 hours ago
This is great, really interesting answer.
– user3483203
5 hours ago
1
1
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
Hi Paul, this answer is really elegant, but the identity answers seem specific to the case where the N=M.shape[0]=M.shape[1]. Is the solution similarly elegant for N=/=M.shape[0]=/=M.shape[1]? Thanks for the answer, learning a lot!
– seveibar
4 hours ago
1
1
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
@seveibar I've updated the answer. It is really just a matter of replacing the correct 5s with Ns.
– Paul Panzer
4 hours ago
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2f55543949%2fhow-to-efficiently-unroll-a-matrix-by-value-with-numpy%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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
It would be better if you explain it in detail.
– Marios Nikolaou
7 hours ago
2
@MariosNikolaou just copy/paste his code and
print(M)
;print(A)
...I edited it for you though– Reedinationer
7 hours ago
@Reedinationer i did it.
– Marios Nikolaou
7 hours ago
I would not recommend pasting output for this code as the input is randomised without a seed.
– coldspeed
7 hours ago
1
i == M
compare int with array 5x5 ? and then save it in A?– Marios Nikolaou
7 hours ago