Set every element to zero in matrix unless it's `1` or `1/2`
$begingroup$
I have the following code:
max = 1;
PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm
That generates this matrix:
$$ $$">
How do I set every element that is not equal to 1
or 1/2
to 0
?
list-manipulation matrix
$endgroup$
add a comment |
$begingroup$
I have the following code:
max = 1;
PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm
That generates this matrix:
$$ $$">
How do I set every element that is not equal to 1
or 1/2
to 0
?
list-manipulation matrix
$endgroup$
$begingroup$
Something likea /. {1 -> 0, 1/2 -> 0}
should do it.
$endgroup$
– Carl Lange
1 hour ago
$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Also, your command does not change anything to mya
$endgroup$
– SuperCiocia
1 hour ago
1
$begingroup$
Look upExcept
. Also, if you want to changea
, you have to assign the result toa
again. (In Mathematica, barely anything changes variables, most things are immutable)
$endgroup$
– Lukas Lang
1 hour ago
add a comment |
$begingroup$
I have the following code:
max = 1;
PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm
That generates this matrix:
$$ $$">
How do I set every element that is not equal to 1
or 1/2
to 0
?
list-manipulation matrix
$endgroup$
I have the following code:
max = 1;
PotentialTilde[V0_] :=
SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1 }];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]] //
MatrixForm
That generates this matrix:
$$ $$">
How do I set every element that is not equal to 1
or 1/2
to 0
?
list-manipulation matrix
list-manipulation matrix
edited 36 mins ago
gwr
7,81322558
7,81322558
asked 1 hour ago
SuperCiociaSuperCiocia
515311
515311
$begingroup$
Something likea /. {1 -> 0, 1/2 -> 0}
should do it.
$endgroup$
– Carl Lange
1 hour ago
$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Also, your command does not change anything to mya
$endgroup$
– SuperCiocia
1 hour ago
1
$begingroup$
Look upExcept
. Also, if you want to changea
, you have to assign the result toa
again. (In Mathematica, barely anything changes variables, most things are immutable)
$endgroup$
– Lukas Lang
1 hour ago
add a comment |
$begingroup$
Something likea /. {1 -> 0, 1/2 -> 0}
should do it.
$endgroup$
– Carl Lange
1 hour ago
$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Also, your command does not change anything to mya
$endgroup$
– SuperCiocia
1 hour ago
1
$begingroup$
Look upExcept
. Also, if you want to changea
, you have to assign the result toa
again. (In Mathematica, barely anything changes variables, most things are immutable)
$endgroup$
– Lukas Lang
1 hour ago
$begingroup$
Something like
a /. {1 -> 0, 1/2 -> 0}
should do it.$endgroup$
– Carl Lange
1 hour ago
$begingroup$
Something like
a /. {1 -> 0, 1/2 -> 0}
should do it.$endgroup$
– Carl Lange
1 hour ago
$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Also, your command does not change anything to my
a
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Also, your command does not change anything to my
a
$endgroup$
– SuperCiocia
1 hour ago
1
1
$begingroup$
Look up
Except
. Also, if you want to change a
, you have to assign the result to a
again. (In Mathematica, barely anything changes variables, most things are immutable)$endgroup$
– Lukas Lang
1 hour ago
$begingroup$
Look up
Except
. Also, if you want to change a
, you have to assign the result to a
again. (In Mathematica, barely anything changes variables, most things are immutable)$endgroup$
– Lukas Lang
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
Be careful with assigning a "form" (e.g. MatrixForm
) to a variable. Another observation is that a
is a SparseArray
uses rules to store the values and since ReplaceAll
works with the FullForm
of an expression care has to be taken (using Normal
will make the array a regular matrix again).
This should work:
max = 1;
PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
(* a // MatrixForm *)
aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
aMod // MatrixForm
Lukas Lang's suggestion is even nicer:
aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;
$endgroup$
$begingroup$
Huh, never applyNormal
toSparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)
$endgroup$
– Henrik Schumacher
26 mins ago
add a comment |
$begingroup$
a cannot be a MatrixForm:
(a = KroneckerProduct[PotentialTilde[1],
PotentialTilde[1]]) // MatrixForm
Convert to a regular Matrix:
Normal[a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :> 0}
(a = Normal[
a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :>
0}) // MatrixForm
a
MatrixForm[a]
$endgroup$
add a comment |
$begingroup$
A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
B // MatrixForm
$endgroup$
add a 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');
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%2fmathematica.stackexchange.com%2fquestions%2f189758%2fset-every-element-to-zero-in-matrix-unless-its-1-or-1-2%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
$begingroup$
Be careful with assigning a "form" (e.g. MatrixForm
) to a variable. Another observation is that a
is a SparseArray
uses rules to store the values and since ReplaceAll
works with the FullForm
of an expression care has to be taken (using Normal
will make the array a regular matrix again).
This should work:
max = 1;
PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
(* a // MatrixForm *)
aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
aMod // MatrixForm
Lukas Lang's suggestion is even nicer:
aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;
$endgroup$
$begingroup$
Huh, never applyNormal
toSparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)
$endgroup$
– Henrik Schumacher
26 mins ago
add a comment |
$begingroup$
Be careful with assigning a "form" (e.g. MatrixForm
) to a variable. Another observation is that a
is a SparseArray
uses rules to store the values and since ReplaceAll
works with the FullForm
of an expression care has to be taken (using Normal
will make the array a regular matrix again).
This should work:
max = 1;
PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
(* a // MatrixForm *)
aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
aMod // MatrixForm
Lukas Lang's suggestion is even nicer:
aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;
$endgroup$
$begingroup$
Huh, never applyNormal
toSparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)
$endgroup$
– Henrik Schumacher
26 mins ago
add a comment |
$begingroup$
Be careful with assigning a "form" (e.g. MatrixForm
) to a variable. Another observation is that a
is a SparseArray
uses rules to store the values and since ReplaceAll
works with the FullForm
of an expression care has to be taken (using Normal
will make the array a regular matrix again).
This should work:
max = 1;
PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
(* a // MatrixForm *)
aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
aMod // MatrixForm
Lukas Lang's suggestion is even nicer:
aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;
$endgroup$
Be careful with assigning a "form" (e.g. MatrixForm
) to a variable. Another observation is that a
is a SparseArray
uses rules to store the values and since ReplaceAll
works with the FullForm
of an expression care has to be taken (using Normal
will make the array a regular matrix again).
This should work:
max = 1;
PotentialTilde[V0_] := SparseArray[{Band[{1, 1}] -> V0/1, Band[{2, 1}] -> V0/2,
Band[{1, 2}] -> V0/2}, {2*max + 1, 2*max + 1}];
a = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
(* a // MatrixForm *)
aMod = (a // Normal) /. x_?NumericQ /; Not@MatchQ[x, 1 | 1/2] -> 0;
aMod // MatrixForm
Lukas Lang's suggestion is even nicer:
aMod = (a // Normal) /. Except[1 | 1/2, _?NumericQ] -> 0;
edited 48 mins ago
answered 1 hour ago
gwrgwr
7,81322558
7,81322558
$begingroup$
Huh, never applyNormal
toSparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)
$endgroup$
– Henrik Schumacher
26 mins ago
add a comment |
$begingroup$
Huh, never applyNormal
toSparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)
$endgroup$
– Henrik Schumacher
26 mins ago
$begingroup$
Huh, never apply
Normal
to SparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)$endgroup$
– Henrik Schumacher
26 mins ago
$begingroup$
Huh, never apply
Normal
to SparseArray
s. Hell might break loose... ;) (Anyways, +1 of course.)$endgroup$
– Henrik Schumacher
26 mins ago
add a comment |
$begingroup$
a cannot be a MatrixForm:
(a = KroneckerProduct[PotentialTilde[1],
PotentialTilde[1]]) // MatrixForm
Convert to a regular Matrix:
Normal[a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :> 0}
(a = Normal[
a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :>
0}) // MatrixForm
a
MatrixForm[a]
$endgroup$
add a comment |
$begingroup$
a cannot be a MatrixForm:
(a = KroneckerProduct[PotentialTilde[1],
PotentialTilde[1]]) // MatrixForm
Convert to a regular Matrix:
Normal[a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :> 0}
(a = Normal[
a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :>
0}) // MatrixForm
a
MatrixForm[a]
$endgroup$
add a comment |
$begingroup$
a cannot be a MatrixForm:
(a = KroneckerProduct[PotentialTilde[1],
PotentialTilde[1]]) // MatrixForm
Convert to a regular Matrix:
Normal[a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :> 0}
(a = Normal[
a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :>
0}) // MatrixForm
a
MatrixForm[a]
$endgroup$
a cannot be a MatrixForm:
(a = KroneckerProduct[PotentialTilde[1],
PotentialTilde[1]]) // MatrixForm
Convert to a regular Matrix:
Normal[a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :> 0}
(a = Normal[
a] /. {ij_ /; (NumberQ[
ij] && ( ij != 1/2 && ij != 1)) :>
0}) // MatrixForm
a
MatrixForm[a]
answered 40 mins ago
Craig CarterCraig Carter
499412
499412
add a comment |
add a comment |
$begingroup$
A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
B // MatrixForm
$endgroup$
add a comment |
$begingroup$
A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
B // MatrixForm
$endgroup$
add a comment |
$begingroup$
A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
B // MatrixForm
$endgroup$
A = KroneckerProduct[PotentialTilde[1], PotentialTilde[1]];
B = A (1 - Unitize[A - 1/2] Unitize[A - 1]);
B // MatrixForm
edited 16 mins ago
answered 33 mins ago
Henrik SchumacherHenrik Schumacher
50.5k469144
50.5k469144
add a comment |
add a 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');
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%2fmathematica.stackexchange.com%2fquestions%2f189758%2fset-every-element-to-zero-in-matrix-unless-its-1-or-1-2%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
$begingroup$
Something like
a /. {1 -> 0, 1/2 -> 0}
should do it.$endgroup$
– Carl Lange
1 hour ago
$begingroup$
Yeah, but I want the opposite. Anything that is NOT equal to 1 and 1/2
$endgroup$
– SuperCiocia
1 hour ago
$begingroup$
Also, your command does not change anything to my
a
$endgroup$
– SuperCiocia
1 hour ago
1
$begingroup$
Look up
Except
. Also, if you want to changea
, you have to assign the result toa
again. (In Mathematica, barely anything changes variables, most things are immutable)$endgroup$
– Lukas Lang
1 hour ago