Manipulate a list: replace random position of a specific integer n times by 0Avoid making repeated Random Integer in different iterationsRandom Integer SequencesModifying the elements of a list according to a given probabilityhow to update elements in a matrix with position identified in a listHow to Floor specific entries of list within a matrix?Replacing elements of a list using a pure function fails; why?List manipulation with random elements under limiting conditionsHow to split sublists into sub-sublists without merging the sublistsReplace single element of a nested list with multiple elementsColoring elements in nested list consistently over all sublists
Polyhedra, Polyhedron, Polytopes and Polygon
Why did Windows 95 crash the whole system but newer Windows only crashed programs?
How to store my pliers and wire cutters on my desk?
Compound Word Neologism
Why didn’t Christianity spread southwards from Ethiopia in the Middle Ages?
Are the named pipe created by `mknod` and the FIFO created by `mkfifo` equivalent?
How to efficiently shred a lot of cabbage?
Dobbs Murder Mystery : A Picture worth 1000 words?
Japanese reading of an integer
Why does the Eurostar not show youth pricing?
Does Dispel Magic destroy Artificer Turrets?
How can Paypal know my card is being used in another account?
Is it okay for me to decline a project on ethical grounds?
Why is it considered acid rain with pH <5.6?
2 weeks and a tight budget to prepare for Z-day. How long can I hunker down?
Self-deportation of American Citizens from US
Assuring luggage isn't lost with short layover
Incrementing add under condition in pandas
What container to use to store developer concentrate?
How did the Axis intend to hold the Caucasus?
Does Wolfram Mathworld make a mistake describing a discrete probability distribution with a probability density function?
Do the books ever say oliphaunts aren’t elephants?
What steps would an amateur scientist have to take in order to get a scientific breakthrough published?
What would the United Kingdom's "optimal" Brexit deal look like?
Manipulate a list: replace random position of a specific integer n times by 0
Avoid making repeated Random Integer in different iterationsRandom Integer SequencesModifying the elements of a list according to a given probabilityhow to update elements in a matrix with position identified in a listHow to Floor specific entries of list within a matrix?Replacing elements of a list using a pure function fails; why?List manipulation with random elements under limiting conditionsHow to split sublists into sub-sublists without merging the sublistsReplace single element of a nested list with multiple elementsColoring elements in nested list consistently over all sublists
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I have this matrix with 3 sublists:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.
The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:
new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Any help appreciated.
list-manipulation matrix random
$endgroup$
add a comment |
$begingroup$
I have this matrix with 3 sublists:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.
The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:
new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Any help appreciated.
list-manipulation matrix random
$endgroup$
add a comment |
$begingroup$
I have this matrix with 3 sublists:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.
The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:
new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Any help appreciated.
list-manipulation matrix random
$endgroup$
I have this matrix with 3 sublists:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
For learning purposes, I try to replace integer 1 (randomly 3 times) in each sublist with 0.
The output can be like this after replace number 1 in a random position of each sublist by 0 tree times:
new d = 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Any help appreciated.
list-manipulation matrix random
list-manipulation matrix random
edited Jul 19 at 6:38
user64494
4,1562 gold badges14 silver badges23 bronze badges
4,1562 gold badges14 silver badges23 bronze badges
asked Jul 18 at 21:03
Ben AawfBen Aawf
908 bronze badges
908 bronze badges
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
Use ReplacePart
:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
f = ReplacePart[#, RandomSample[Position[#, 1], 3] -> 0]&
Print[f /@ d]
(* 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
Try it online!
If you want it to work on lists that contain fewer than 3 1s, replacing them all with 0s, use UpTo[3]
instead of 3
.
$endgroup$
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
@BenAawfMapAt
(used in klgr's answer) is more general thanReplacePart
; you can do for instancef = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.
$endgroup$
– lirtosiast
Jul 18 at 21:43
add a comment |
$begingroup$
MapAt[0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Module[s= SparseArray[RandomSample[Position[#, 1], UpTo@3] -> 0, Length@#, 1], s #] & /@ d
0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
SubsetMap[0, 0, 0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
add a comment |
$begingroup$
A function that takes a list and replaces a random 1 by a 0:
replaceone[L_] := ReplacePart[L, RandomChoice[Position[L, 1]][[1]] -> 0]
Apply it three times to all sublists of d
:
Nest[replaceone, #, 3] & /@ d
(* 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
$endgroup$
add a comment |
$begingroup$
I guess I'll throw my hat in the ring.
f[list_, numtorep_, numofreps_] := Block[
data = list,
Apply[
(data[[##]] = 0) &,
RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
2
];
data
]
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
f[d, 1, 3]
1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
add a comment |
Your Answer
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%2f202332%2fmanipulate-a-list-replace-random-position-of-a-specific-integer-n-times-by-0%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Use ReplacePart
:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
f = ReplacePart[#, RandomSample[Position[#, 1], 3] -> 0]&
Print[f /@ d]
(* 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
Try it online!
If you want it to work on lists that contain fewer than 3 1s, replacing them all with 0s, use UpTo[3]
instead of 3
.
$endgroup$
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
@BenAawfMapAt
(used in klgr's answer) is more general thanReplacePart
; you can do for instancef = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.
$endgroup$
– lirtosiast
Jul 18 at 21:43
add a comment |
$begingroup$
Use ReplacePart
:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
f = ReplacePart[#, RandomSample[Position[#, 1], 3] -> 0]&
Print[f /@ d]
(* 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
Try it online!
If you want it to work on lists that contain fewer than 3 1s, replacing them all with 0s, use UpTo[3]
instead of 3
.
$endgroup$
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
@BenAawfMapAt
(used in klgr's answer) is more general thanReplacePart
; you can do for instancef = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.
$endgroup$
– lirtosiast
Jul 18 at 21:43
add a comment |
$begingroup$
Use ReplacePart
:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
f = ReplacePart[#, RandomSample[Position[#, 1], 3] -> 0]&
Print[f /@ d]
(* 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
Try it online!
If you want it to work on lists that contain fewer than 3 1s, replacing them all with 0s, use UpTo[3]
instead of 3
.
$endgroup$
Use ReplacePart
:
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0;
f = ReplacePart[#, RandomSample[Position[#, 1], 3] -> 0]&
Print[f /@ d]
(* 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
Try it online!
If you want it to work on lists that contain fewer than 3 1s, replacing them all with 0s, use UpTo[3]
instead of 3
.
answered Jul 18 at 21:22
lirtosiastlirtosiast
4672 silver badges12 bronze badges
4672 silver badges12 bronze badges
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
@BenAawfMapAt
(used in klgr's answer) is more general thanReplacePart
; you can do for instancef = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.
$endgroup$
– lirtosiast
Jul 18 at 21:43
add a comment |
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
@BenAawfMapAt
(used in klgr's answer) is more general thanReplacePart
; you can do for instancef = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.
$endgroup$
– lirtosiast
Jul 18 at 21:43
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
That's great.#lirtosiast. One question more, please. How I can use If function. I want to pick randomly 3 elements from each sublist in matrix d and replace by if 0->1, or 1->0
$endgroup$
– Ben Aawf
Jul 18 at 21:39
$begingroup$
@BenAawf
MapAt
(used in klgr's answer) is more general than ReplacePart
; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.$endgroup$
– lirtosiast
Jul 18 at 21:43
$begingroup$
@BenAawf
MapAt
(used in klgr's answer) is more general than ReplacePart
; you can do for instance f = MapAt[1-#&, #, RandomSample[Position[#, 0 | 1], 3]]&
, which does your replacement and leaves all elements not 0 or 1 unchanged.$endgroup$
– lirtosiast
Jul 18 at 21:43
add a comment |
$begingroup$
MapAt[0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Module[s= SparseArray[RandomSample[Position[#, 1], UpTo@3] -> 0, Length@#, 1], s #] & /@ d
0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
SubsetMap[0, 0, 0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
add a comment |
$begingroup$
MapAt[0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Module[s= SparseArray[RandomSample[Position[#, 1], UpTo@3] -> 0, Length@#, 1], s #] & /@ d
0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
SubsetMap[0, 0, 0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
add a comment |
$begingroup$
MapAt[0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Module[s= SparseArray[RandomSample[Position[#, 1], UpTo@3] -> 0, Length@#, 1], s #] & /@ d
0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
SubsetMap[0, 0, 0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
MapAt[0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Module[s= SparseArray[RandomSample[Position[#, 1], UpTo@3] -> 0, Length@#, 1], s #] & /@ d
0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
SubsetMap[0, 0, 0 &, #, RandomSample[Position[#, 1], UpTo@3]] & /@ d
1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
edited Jul 18 at 22:49
answered Jul 18 at 21:23
kglrkglr
208k10 gold badges238 silver badges472 bronze badges
208k10 gold badges238 silver badges472 bronze badges
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
add a comment |
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
$begingroup$
wooow that's so helpful thank you so much @kglr
$endgroup$
– Ben Aawf
Jul 18 at 22:06
add a comment |
$begingroup$
A function that takes a list and replaces a random 1 by a 0:
replaceone[L_] := ReplacePart[L, RandomChoice[Position[L, 1]][[1]] -> 0]
Apply it three times to all sublists of d
:
Nest[replaceone, #, 3] & /@ d
(* 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
$endgroup$
add a comment |
$begingroup$
A function that takes a list and replaces a random 1 by a 0:
replaceone[L_] := ReplacePart[L, RandomChoice[Position[L, 1]][[1]] -> 0]
Apply it three times to all sublists of d
:
Nest[replaceone, #, 3] & /@ d
(* 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
$endgroup$
add a comment |
$begingroup$
A function that takes a list and replaces a random 1 by a 0:
replaceone[L_] := ReplacePart[L, RandomChoice[Position[L, 1]][[1]] -> 0]
Apply it three times to all sublists of d
:
Nest[replaceone, #, 3] & /@ d
(* 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
$endgroup$
A function that takes a list and replaces a random 1 by a 0:
replaceone[L_] := ReplacePart[L, RandomChoice[Position[L, 1]][[1]] -> 0]
Apply it three times to all sublists of d
:
Nest[replaceone, #, 3] & /@ d
(* 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 *)
edited Jul 18 at 21:22
answered Jul 18 at 21:16
RomanRoman
14.9k1 gold badge20 silver badges51 bronze badges
14.9k1 gold badge20 silver badges51 bronze badges
add a comment |
add a comment |
$begingroup$
I guess I'll throw my hat in the ring.
f[list_, numtorep_, numofreps_] := Block[
data = list,
Apply[
(data[[##]] = 0) &,
RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
2
];
data
]
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
f[d, 1, 3]
1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
add a comment |
$begingroup$
I guess I'll throw my hat in the ring.
f[list_, numtorep_, numofreps_] := Block[
data = list,
Apply[
(data[[##]] = 0) &,
RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
2
];
data
]
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
f[d, 1, 3]
1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
add a comment |
$begingroup$
I guess I'll throw my hat in the ring.
f[list_, numtorep_, numofreps_] := Block[
data = list,
Apply[
(data[[##]] = 0) &,
RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
2
];
data
]
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
f[d, 1, 3]
1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
$endgroup$
I guess I'll throw my hat in the ring.
f[list_, numtorep_, numofreps_] := Block[
data = list,
Apply[
(data[[##]] = 0) &,
RandomSample[#, numofreps] & /@ GatherBy[Position[list, numtorep], First],
2
];
data
]
d = 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
f[d, 1, 3]
1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1,
1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0
0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
answered Jul 18 at 22:38
That Gravity GuyThat Gravity Guy
2,3211 gold badge6 silver badges15 bronze badges
2,3211 gold badge6 silver badges15 bronze badges
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');
);
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%2f202332%2fmanipulate-a-list-replace-random-position-of-a-specific-integer-n-times-by-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