Subtract a column vector from matrix at specified vector of columns using only broadcast
I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg.
M = array([[ 1, 2, 1, 1],
[ 2, 1, 1, 1],
[ 1, 1, 2, 1],
[ 2, 1, 1, 1],
[ 1, 1, 1, 2]]) # An example matrix
V = array([1, 1, 1, 1, 1]) # An example column vector
I = array([0, 3, 2, 3, 1, 3, 3]) # The index maxtrix
Now I want to subtract V from M at column numbers given in I.
For eg. I[0] is 0, so subtract V from first column (zero index) of matrix M.
Similarly I[1] = 3, subtract V from fourth column (three index) of matrix M.
At the end of operation, since 3 occurs 4 times in I, so V will be subtracted from third column i.e. last column of M- 4 times.
I need to do this using only broadcast, no loops.
I have tried the following:
M[:, I] - V[np.newaxis, :].T
but it ends up broadcasting resultant matrix to have more columns than there are in M.
python numpy matrix numpy-broadcasting
add a comment |
I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg.
M = array([[ 1, 2, 1, 1],
[ 2, 1, 1, 1],
[ 1, 1, 2, 1],
[ 2, 1, 1, 1],
[ 1, 1, 1, 2]]) # An example matrix
V = array([1, 1, 1, 1, 1]) # An example column vector
I = array([0, 3, 2, 3, 1, 3, 3]) # The index maxtrix
Now I want to subtract V from M at column numbers given in I.
For eg. I[0] is 0, so subtract V from first column (zero index) of matrix M.
Similarly I[1] = 3, subtract V from fourth column (three index) of matrix M.
At the end of operation, since 3 occurs 4 times in I, so V will be subtracted from third column i.e. last column of M- 4 times.
I need to do this using only broadcast, no loops.
I have tried the following:
M[:, I] - V[np.newaxis, :].T
but it ends up broadcasting resultant matrix to have more columns than there are in M.
python numpy matrix numpy-broadcasting
add a comment |
I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg.
M = array([[ 1, 2, 1, 1],
[ 2, 1, 1, 1],
[ 1, 1, 2, 1],
[ 2, 1, 1, 1],
[ 1, 1, 1, 2]]) # An example matrix
V = array([1, 1, 1, 1, 1]) # An example column vector
I = array([0, 3, 2, 3, 1, 3, 3]) # The index maxtrix
Now I want to subtract V from M at column numbers given in I.
For eg. I[0] is 0, so subtract V from first column (zero index) of matrix M.
Similarly I[1] = 3, subtract V from fourth column (three index) of matrix M.
At the end of operation, since 3 occurs 4 times in I, so V will be subtracted from third column i.e. last column of M- 4 times.
I need to do this using only broadcast, no loops.
I have tried the following:
M[:, I] - V[np.newaxis, :].T
but it ends up broadcasting resultant matrix to have more columns than there are in M.
python numpy matrix numpy-broadcasting
I want to subtract a column vector from a numpy matrix using another vector which is index of columns where the first column vector needs to be subtracted from the main matrix. For eg.
M = array([[ 1, 2, 1, 1],
[ 2, 1, 1, 1],
[ 1, 1, 2, 1],
[ 2, 1, 1, 1],
[ 1, 1, 1, 2]]) # An example matrix
V = array([1, 1, 1, 1, 1]) # An example column vector
I = array([0, 3, 2, 3, 1, 3, 3]) # The index maxtrix
Now I want to subtract V from M at column numbers given in I.
For eg. I[0] is 0, so subtract V from first column (zero index) of matrix M.
Similarly I[1] = 3, subtract V from fourth column (three index) of matrix M.
At the end of operation, since 3 occurs 4 times in I, so V will be subtracted from third column i.e. last column of M- 4 times.
I need to do this using only broadcast, no loops.
I have tried the following:
M[:, I] - V[np.newaxis, :].T
but it ends up broadcasting resultant matrix to have more columns than there are in M.
python numpy matrix numpy-broadcasting
python numpy matrix numpy-broadcasting
edited Nov 14 '18 at 7:47
Henrik
41.5k994109
41.5k994109
asked Nov 14 '18 at 7:14
Varun KuntalVarun Kuntal
234
234
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
One can use bincount
and outer
>>> M - np.outer(V, np.bincount(I, None, M.shape[1]))
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
or subtract.at
>>> out = M.copy()
>>> np.subtract.at(out, (np.s_[:], I), V[:, None])
>>> out
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
bincount
should be more performant!
– Divakar
Nov 14 '18 at 7:43
add a comment |
We can use np.subtract.at
on transposed view of M
-
np.subtract.at(M.T,I,V)
extendingI
seems unnecessary - still +1 for brevity
– Paul Panzer
Nov 14 '18 at 7:35
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
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%2f53294889%2fsubtract-a-column-vector-from-matrix-at-specified-vector-of-columns-using-only-b%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
One can use bincount
and outer
>>> M - np.outer(V, np.bincount(I, None, M.shape[1]))
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
or subtract.at
>>> out = M.copy()
>>> np.subtract.at(out, (np.s_[:], I), V[:, None])
>>> out
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
bincount
should be more performant!
– Divakar
Nov 14 '18 at 7:43
add a comment |
One can use bincount
and outer
>>> M - np.outer(V, np.bincount(I, None, M.shape[1]))
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
or subtract.at
>>> out = M.copy()
>>> np.subtract.at(out, (np.s_[:], I), V[:, None])
>>> out
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
bincount
should be more performant!
– Divakar
Nov 14 '18 at 7:43
add a comment |
One can use bincount
and outer
>>> M - np.outer(V, np.bincount(I, None, M.shape[1]))
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
or subtract.at
>>> out = M.copy()
>>> np.subtract.at(out, (np.s_[:], I), V[:, None])
>>> out
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
One can use bincount
and outer
>>> M - np.outer(V, np.bincount(I, None, M.shape[1]))
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
or subtract.at
>>> out = M.copy()
>>> np.subtract.at(out, (np.s_[:], I), V[:, None])
>>> out
array([[ 0, 1, 0, -3],
[ 1, 0, 0, -3],
[ 0, 0, 1, -3],
[ 1, 0, 0, -3],
[ 0, 0, 0, -2]])
edited Nov 14 '18 at 7:32
answered Nov 14 '18 at 7:27
Paul PanzerPaul Panzer
30.1k21240
30.1k21240
bincount
should be more performant!
– Divakar
Nov 14 '18 at 7:43
add a comment |
bincount
should be more performant!
– Divakar
Nov 14 '18 at 7:43
bincount
should be more performant!– Divakar
Nov 14 '18 at 7:43
bincount
should be more performant!– Divakar
Nov 14 '18 at 7:43
add a comment |
We can use np.subtract.at
on transposed view of M
-
np.subtract.at(M.T,I,V)
extendingI
seems unnecessary - still +1 for brevity
– Paul Panzer
Nov 14 '18 at 7:35
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
add a comment |
We can use np.subtract.at
on transposed view of M
-
np.subtract.at(M.T,I,V)
extendingI
seems unnecessary - still +1 for brevity
– Paul Panzer
Nov 14 '18 at 7:35
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
add a comment |
We can use np.subtract.at
on transposed view of M
-
np.subtract.at(M.T,I,V)
We can use np.subtract.at
on transposed view of M
-
np.subtract.at(M.T,I,V)
edited Nov 14 '18 at 7:42
answered Nov 14 '18 at 7:23
DivakarDivakar
156k1485177
156k1485177
extendingI
seems unnecessary - still +1 for brevity
– Paul Panzer
Nov 14 '18 at 7:35
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
add a comment |
extendingI
seems unnecessary - still +1 for brevity
– Paul Panzer
Nov 14 '18 at 7:35
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
extending
I
seems unnecessary - still +1 for brevity– Paul Panzer
Nov 14 '18 at 7:35
extending
I
seems unnecessary - still +1 for brevity– Paul Panzer
Nov 14 '18 at 7:35
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
@PaulPanzer Yup, you are right there!
– Divakar
Nov 14 '18 at 7:42
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%2f53294889%2fsubtract-a-column-vector-from-matrix-at-specified-vector-of-columns-using-only-b%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