using DeleteCases to delete vectors with particular index as 0
Assume I have a set of vectors
set = {{0,1,1,0,1}, {1,0,1,1,1}, {1,1,1,1,0},{1,0,0,1,0}}.
I want to delete the vectors which have the last entry as 0. In the above example, it is the 3rd and 4th vectors.
Can I use DeleteCases in a better way than following?
Do[If[set[[j]][[5]] == 0, set[[j]] = ConstantArray[0, Length[set[[1]]]]],
{j, 1, Length[set]}];
set = DeleteCases[set, ConstantArray[0, Length[set[[1]]]]];
matrix vector column deletecases
add a comment |
Assume I have a set of vectors
set = {{0,1,1,0,1}, {1,0,1,1,1}, {1,1,1,1,0},{1,0,0,1,0}}.
I want to delete the vectors which have the last entry as 0. In the above example, it is the 3rd and 4th vectors.
Can I use DeleteCases in a better way than following?
Do[If[set[[j]][[5]] == 0, set[[j]] = ConstantArray[0, Length[set[[1]]]]],
{j, 1, Length[set]}];
set = DeleteCases[set, ConstantArray[0, Length[set[[1]]]]];
matrix vector column deletecases
add a comment |
Assume I have a set of vectors
set = {{0,1,1,0,1}, {1,0,1,1,1}, {1,1,1,1,0},{1,0,0,1,0}}.
I want to delete the vectors which have the last entry as 0. In the above example, it is the 3rd and 4th vectors.
Can I use DeleteCases in a better way than following?
Do[If[set[[j]][[5]] == 0, set[[j]] = ConstantArray[0, Length[set[[1]]]]],
{j, 1, Length[set]}];
set = DeleteCases[set, ConstantArray[0, Length[set[[1]]]]];
matrix vector column deletecases
Assume I have a set of vectors
set = {{0,1,1,0,1}, {1,0,1,1,1}, {1,1,1,1,0},{1,0,0,1,0}}.
I want to delete the vectors which have the last entry as 0. In the above example, it is the 3rd and 4th vectors.
Can I use DeleteCases in a better way than following?
Do[If[set[[j]][[5]] == 0, set[[j]] = ConstantArray[0, Length[set[[1]]]]],
{j, 1, Length[set]}];
set = DeleteCases[set, ConstantArray[0, Length[set[[1]]]]];
matrix vector column deletecases
matrix vector column deletecases
asked 6 hours ago
cleanplaycleanplay
30918
30918
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
DeleteCases[set, {___, 0}]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
In this case Pick
also works:
Pick[set, Last /@ set, 1]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
You asked about positions other than last. It is possible to generate a working pattern using Blank
, e.g. {_, _, 0, _, _}
, and this process can be automated, but that is not usually the first approach I would recommend.
You can use individual part extraction in either Select
or Cases
/DeleteCases
, or you can create a "mask" for use with Pick
as I showed above. Notable differences are that Select
only operates at a single level, while Cases
and Pick
generalize to deeper structures. Pick
is often somewhat faster than other methods, especially if one uses fast numeric functions where possible.
Picking according to the second column:
Pick[set, set[[All, 2]], 1]
{{0, 1, 1, 0, 1}, {1, 1, 1, 1, 0}}
Suppose however that your keep elements are not all the same, like 1
in this example above:
set2 = {{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}, {2, 3, 0, 0, 1}, {3, 1, 0, 4, 4}};
Pick[set2, Unitize @ set2[[All, 3]], 1]
{{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}}
Note here that Unitize
is needed to make all keep elements into 1
. While it is possible to use e.g. Positive
instead this is not as efficient because in Mathematica Booleans True
and False
cannot be packed, while machine-size integers can. It is for this reason that I do not recommend these apparent equivalents:
(* Not recommended where performance matters *)
Pick[set2, Positive @ set2[[All, 3]]]
Pick[set2, set2[[All, 3]], _?Positive]
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
1
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
add a comment |
Or...
Select[set, Last[#] != 0 &]
or...
Select[set, #[[-1]] != 0 &]
or...
DeleteCases[set, x_ /; Last[x] == 0]
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');
});
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%2f189089%2fusing-deletecases-to-delete-vectors-with-particular-index-as-0%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
DeleteCases[set, {___, 0}]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
In this case Pick
also works:
Pick[set, Last /@ set, 1]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
You asked about positions other than last. It is possible to generate a working pattern using Blank
, e.g. {_, _, 0, _, _}
, and this process can be automated, but that is not usually the first approach I would recommend.
You can use individual part extraction in either Select
or Cases
/DeleteCases
, or you can create a "mask" for use with Pick
as I showed above. Notable differences are that Select
only operates at a single level, while Cases
and Pick
generalize to deeper structures. Pick
is often somewhat faster than other methods, especially if one uses fast numeric functions where possible.
Picking according to the second column:
Pick[set, set[[All, 2]], 1]
{{0, 1, 1, 0, 1}, {1, 1, 1, 1, 0}}
Suppose however that your keep elements are not all the same, like 1
in this example above:
set2 = {{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}, {2, 3, 0, 0, 1}, {3, 1, 0, 4, 4}};
Pick[set2, Unitize @ set2[[All, 3]], 1]
{{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}}
Note here that Unitize
is needed to make all keep elements into 1
. While it is possible to use e.g. Positive
instead this is not as efficient because in Mathematica Booleans True
and False
cannot be packed, while machine-size integers can. It is for this reason that I do not recommend these apparent equivalents:
(* Not recommended where performance matters *)
Pick[set2, Positive @ set2[[All, 3]]]
Pick[set2, set2[[All, 3]], _?Positive]
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
1
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
add a comment |
DeleteCases[set, {___, 0}]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
In this case Pick
also works:
Pick[set, Last /@ set, 1]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
You asked about positions other than last. It is possible to generate a working pattern using Blank
, e.g. {_, _, 0, _, _}
, and this process can be automated, but that is not usually the first approach I would recommend.
You can use individual part extraction in either Select
or Cases
/DeleteCases
, or you can create a "mask" for use with Pick
as I showed above. Notable differences are that Select
only operates at a single level, while Cases
and Pick
generalize to deeper structures. Pick
is often somewhat faster than other methods, especially if one uses fast numeric functions where possible.
Picking according to the second column:
Pick[set, set[[All, 2]], 1]
{{0, 1, 1, 0, 1}, {1, 1, 1, 1, 0}}
Suppose however that your keep elements are not all the same, like 1
in this example above:
set2 = {{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}, {2, 3, 0, 0, 1}, {3, 1, 0, 4, 4}};
Pick[set2, Unitize @ set2[[All, 3]], 1]
{{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}}
Note here that Unitize
is needed to make all keep elements into 1
. While it is possible to use e.g. Positive
instead this is not as efficient because in Mathematica Booleans True
and False
cannot be packed, while machine-size integers can. It is for this reason that I do not recommend these apparent equivalents:
(* Not recommended where performance matters *)
Pick[set2, Positive @ set2[[All, 3]]]
Pick[set2, set2[[All, 3]], _?Positive]
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
1
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
add a comment |
DeleteCases[set, {___, 0}]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
In this case Pick
also works:
Pick[set, Last /@ set, 1]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
You asked about positions other than last. It is possible to generate a working pattern using Blank
, e.g. {_, _, 0, _, _}
, and this process can be automated, but that is not usually the first approach I would recommend.
You can use individual part extraction in either Select
or Cases
/DeleteCases
, or you can create a "mask" for use with Pick
as I showed above. Notable differences are that Select
only operates at a single level, while Cases
and Pick
generalize to deeper structures. Pick
is often somewhat faster than other methods, especially if one uses fast numeric functions where possible.
Picking according to the second column:
Pick[set, set[[All, 2]], 1]
{{0, 1, 1, 0, 1}, {1, 1, 1, 1, 0}}
Suppose however that your keep elements are not all the same, like 1
in this example above:
set2 = {{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}, {2, 3, 0, 0, 1}, {3, 1, 0, 4, 4}};
Pick[set2, Unitize @ set2[[All, 3]], 1]
{{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}}
Note here that Unitize
is needed to make all keep elements into 1
. While it is possible to use e.g. Positive
instead this is not as efficient because in Mathematica Booleans True
and False
cannot be packed, while machine-size integers can. It is for this reason that I do not recommend these apparent equivalents:
(* Not recommended where performance matters *)
Pick[set2, Positive @ set2[[All, 3]]]
Pick[set2, set2[[All, 3]], _?Positive]
DeleteCases[set, {___, 0}]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
In this case Pick
also works:
Pick[set, Last /@ set, 1]
{{0, 1, 1, 0, 1}, {1, 0, 1, 1, 1}}
You asked about positions other than last. It is possible to generate a working pattern using Blank
, e.g. {_, _, 0, _, _}
, and this process can be automated, but that is not usually the first approach I would recommend.
You can use individual part extraction in either Select
or Cases
/DeleteCases
, or you can create a "mask" for use with Pick
as I showed above. Notable differences are that Select
only operates at a single level, while Cases
and Pick
generalize to deeper structures. Pick
is often somewhat faster than other methods, especially if one uses fast numeric functions where possible.
Picking according to the second column:
Pick[set, set[[All, 2]], 1]
{{0, 1, 1, 0, 1}, {1, 1, 1, 1, 0}}
Suppose however that your keep elements are not all the same, like 1
in this example above:
set2 = {{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}, {2, 3, 0, 0, 1}, {3, 1, 0, 4, 4}};
Pick[set2, Unitize @ set2[[All, 3]], 1]
{{3, 1, 2, 1, 4}, {4, 1, 2, 2, 4}}
Note here that Unitize
is needed to make all keep elements into 1
. While it is possible to use e.g. Positive
instead this is not as efficient because in Mathematica Booleans True
and False
cannot be packed, while machine-size integers can. It is for this reason that I do not recommend these apparent equivalents:
(* Not recommended where performance matters *)
Pick[set2, Positive @ set2[[All, 3]]]
Pick[set2, set2[[All, 3]], _?Positive]
edited 5 hours ago
answered 6 hours ago
Mr.Wizard♦Mr.Wizard
230k294741038
230k294741038
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
1
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
add a comment |
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
1
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
Thanks. Can you modify this to any index instead of just the last one?
– cleanplay
5 hours ago
1
1
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
@cleanplay Please see the addendum to my answer.
– Mr.Wizard♦
5 hours ago
add a comment |
Or...
Select[set, Last[#] != 0 &]
or...
Select[set, #[[-1]] != 0 &]
or...
DeleteCases[set, x_ /; Last[x] == 0]
add a comment |
Or...
Select[set, Last[#] != 0 &]
or...
Select[set, #[[-1]] != 0 &]
or...
DeleteCases[set, x_ /; Last[x] == 0]
add a comment |
Or...
Select[set, Last[#] != 0 &]
or...
Select[set, #[[-1]] != 0 &]
or...
DeleteCases[set, x_ /; Last[x] == 0]
Or...
Select[set, Last[#] != 0 &]
or...
Select[set, #[[-1]] != 0 &]
or...
DeleteCases[set, x_ /; Last[x] == 0]
edited 5 hours ago
answered 5 hours ago
David G. StorkDavid G. Stork
23.5k22051
23.5k22051
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fmathematica.stackexchange.com%2fquestions%2f189089%2fusing-deletecases-to-delete-vectors-with-particular-index-as-0%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