Mathematica Asked by Greg Colbourn on May 19, 2021
partstodelete = {{1, 2}, {1, 5}, {1, 6}.. {2, 21}, {2, 28}, {2, 35}..};
Delete[data, partstodelete]
deletes parts partstodelete
from data
.
How do I just get a list of the parts (i.e what is deleted; or the complement of data
and Delete[data, partstodelete]
).
Seems like it should be straightforward using Part
, but data[[partstodelete]]
does not work (Part
doesn’t seem to allow depth of part specifications if giving it a list?)
data = Array[a, {3, 10}];
partstodelete = {{1, 2}, {1, 5}, {1, 6}, {2, 3}, {2, 5}, {2, 8}};
data2 = Delete[data, partstodelete];
Using Part
, the values deleted are
data[[##]] & @@@ partstodelete
(* {a[1, 2], a[1, 5], a[1, 6], a[2, 3], a[2, 5], a[2, 8]} *)
Answered by Bob Hanlon on May 19, 2021
You can define a function that returns (similar to TakeDrop
) both the kept and the deleted parts of input list using Through
+ Delete
+ Extract
:
ClearAll[deleteExtract]
deleteExtract = Through[{ Delete, Extract} @ ##] &;
data = Array[a, {3, 7}];
partstodelete = {{1, 2}, {1, 5}, {1, 6}, {2, 3}, {2, 5}};
MapAt[Framed, data, partstodelete] // TableForm
{kept, deleted} = deleteExtract[data, partstodelete];
kept
{{a[1, 1], a[1, 3], a[1, 4], a[1, 7]}, {a[2, 1], a[2, 2], a[2, 4], a[2, 6], a[2, 7]}, {a[3, 1], a[3, 2], a[3, 3], a[3, 4], a[3, 5], a[3, 6], a[3, 7]}}
deleted
{a[1, 2], a[1, 5], a[1, 6], a[2, 3], a[2, 5]}
data /. { x : Alternatives @@ (Join @@ kept) :> Highlighted[x],
x : Alternatives @@ deleted :> Highlighted[x, Background -> Pink]} // TableForm
Answered by kglr on May 19, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP