给定 2 级元素的值,如何有条件地从嵌套列表的 1 级中删除元素?
How can I conditionally remove elements from level 1 of a nested list given the value of a level 2 element?
平台:Mathematica
我有一个 table 的 x 和 y 坐标属于单独的连接路径(轨迹):
{{轨迹, 帧, x, y}, {1, 0, 158.22, 11.519}, {1, 1, 159.132, 11.637}, ... {6649, 1439, 148.35, 316.144}}
在 table 格式中它看起来像这样:
Trajectory Frame x y
------------------------------------------
1 0 158.22 11.519
1 1 159.13 11.637
1 2 158.507 11.68
1 3 157.971 11.436
1 4 158.435 11.366
1 5 158.626 11.576
2 0 141 12 remove this row, path too short!
2 1 143 15 remove this row, path too short!
2 2 144 16 remove this row, path too short!
2 3 147 18 remove this row, path too short!
3 0 120 400
3 1 121 401
3 2 121 396
3 3 122 394
3 4 121 392
3 5 120 390
3 6 124 388
3 7 125 379
...
我想删除轨迹总长度小于 "n" frames/rows/elements 的任何 elements/rows(本例为 5 帧)。该列表长约 80k 个元素,我想删除所有包含指定阈值以下轨迹的行。
对于给定的示例,轨迹 2 仅存在于 4 帧中,因此我想删除轨迹 2 的所有行。
我是 Mathematica 的新手,我什至不知道从哪里开始。我想也许创建一个列表,其中包含 Count[]
值小于阈值的轨迹编号,然后有条件地用 DeleteCases[]
之类的东西消除任何遵循该模式的元素,但我无法鉴于我有限的语法知识,我走得很远。
感谢您的帮助,期待解决方案!
table = {{"Trajectory", "Frame", "x", "y"},
{1, 0, 158.22, 11.519}, {1, 1, 159.13, 11.637},
{1, 2, 158.507, 11.68}, {1, 3, 157.971, 11.436},
{1, 4, 158.435, 11.366}, {1, 5, 158.626, 11.576},
{2, 0, 141, 12}, {2, 1, 143, 15}, {2, 2, 144, 16},
{2, 3, 147, 18}, {3, 0, 120, 400}, {3, 1, 121, 401},
{3, 2, 121, 396}, {3, 3, 122, 394}, {3, 4, 121, 392},
{3, 5, 120, 390}, {3, 6, 124, 388}, {3, 7, 125, 379}};
traj = First /@ Rest[table];
n = 5;
under = First /@ Select[Tally[traj], Last[#] < n &];
discard = Flatten[Position[table[[All, 1]], #] & /@ under];
newtable = Delete[table, List /@ discard]
或者,对于最后两行,这可能会更快
discard = Position[table[[All, 1]], _?(MemberQ[under, #] &)];
newtable = Delete[table, discard]
平台:Mathematica
我有一个 table 的 x 和 y 坐标属于单独的连接路径(轨迹):
{{轨迹, 帧, x, y}, {1, 0, 158.22, 11.519}, {1, 1, 159.132, 11.637}, ... {6649, 1439, 148.35, 316.144}}
在 table 格式中它看起来像这样:
Trajectory Frame x y
------------------------------------------
1 0 158.22 11.519
1 1 159.13 11.637
1 2 158.507 11.68
1 3 157.971 11.436
1 4 158.435 11.366
1 5 158.626 11.576
2 0 141 12 remove this row, path too short!
2 1 143 15 remove this row, path too short!
2 2 144 16 remove this row, path too short!
2 3 147 18 remove this row, path too short!
3 0 120 400
3 1 121 401
3 2 121 396
3 3 122 394
3 4 121 392
3 5 120 390
3 6 124 388
3 7 125 379
...
我想删除轨迹总长度小于 "n" frames/rows/elements 的任何 elements/rows(本例为 5 帧)。该列表长约 80k 个元素,我想删除所有包含指定阈值以下轨迹的行。
对于给定的示例,轨迹 2 仅存在于 4 帧中,因此我想删除轨迹 2 的所有行。
我是 Mathematica 的新手,我什至不知道从哪里开始。我想也许创建一个列表,其中包含 Count[]
值小于阈值的轨迹编号,然后有条件地用 DeleteCases[]
之类的东西消除任何遵循该模式的元素,但我无法鉴于我有限的语法知识,我走得很远。
感谢您的帮助,期待解决方案!
table = {{"Trajectory", "Frame", "x", "y"},
{1, 0, 158.22, 11.519}, {1, 1, 159.13, 11.637},
{1, 2, 158.507, 11.68}, {1, 3, 157.971, 11.436},
{1, 4, 158.435, 11.366}, {1, 5, 158.626, 11.576},
{2, 0, 141, 12}, {2, 1, 143, 15}, {2, 2, 144, 16},
{2, 3, 147, 18}, {3, 0, 120, 400}, {3, 1, 121, 401},
{3, 2, 121, 396}, {3, 3, 122, 394}, {3, 4, 121, 392},
{3, 5, 120, 390}, {3, 6, 124, 388}, {3, 7, 125, 379}};
traj = First /@ Rest[table];
n = 5;
under = First /@ Select[Tally[traj], Last[#] < n &];
discard = Flatten[Position[table[[All, 1]], #] & /@ under];
newtable = Delete[table, List /@ discard]
或者,对于最后两行,这可能会更快
discard = Position[table[[All, 1]], _?(MemberQ[under, #] &)];
newtable = Delete[table, discard]