自动删除 Mathematica 表中的特定元素

发布于 2024-12-02 20:41:59 字数 2949 浏览 4 评论 0原文

我有一个问题可以分为两个子问题。 我创建了一个表,其代码如下。

问题 1.

xstep = 1;
xmaximum = 6;
numberofxnodes = 6;
numberofynodes = 3;
numberofzlayers = 3;
maximumgridnodes = numberofxnodes*numberofynodes
mnodes = numberofxnodes*numberofynodes*numberofzlayers;
orginaltable = 
   Table[{i, 
          node2 = i + xstep, node3 = node2 + xmaximum,
          node4 = node3 - xstep,node5 = i + maximumgridnodes,
          node6 = node5 + xstep,node7 = node6 + xmaximum,
          node8 = node7 - xstep}, 
        {i, 1, mnodes}]

如果我运行这个,我将得到我的原始表。基本上我想从原始表中删除第六个元素和第六个元素的倍数。我可以使用下面的代码来做到这一点。

 modifiedtable = Drop[orginaltable, {6, mnodes, 6}]

现在我得到修改后的表,其中原始表的每六个元素和第六个元素的倍数都被删除。这解决了我的问题1。

现在我的问题2:

**主要编辑版本**:(上面给出的所有代码都是正确的)

非常感谢您的答案,但我想要其他东西,但我犯了一个错误 在最初解释它时,我正在再尝试一次。

下面是我修改后的表格:我想要中间的元素 “/** 和 **/” 已删除并保留在那里。

{{1, 2, 8, 7, 19, 20, 26, 25}, {2, 3, 9, 8, 20, 21, 27, 26}, {3, 4,10, 9, 21 , 22, 28, 27}, {4, 5, 11, 10, 22, 23, 29, 28}、{5、6、12、11、23、24、30、29}、{7、8、14、13、25、26、32、31}、{8、9、15、14、26、 27, 33, 32}, {9, 10, 16, 15, 27, 28, 34, 33}, {10, 11, 17, 16, 28, 29, 35, 34}, {11, 12, 18, 17, 29, 30, 36, 35}, /**{13, 14 , 20, 19, 31, 32, 38, 37}, {14, 15, 21, 20, 32, 33, 39, 38}, {15, 16, 22, 21, 33, 34, 40, 39}, {16, 17, 23, 22, 34, 35, 41, 40} 、{17、18、24、23、35、 36, 42, 41},**/ {19, 20, 26, 25, 37, 38, 44, 43}, {20, 21, 27, 26, 38, 39, 45, 44}, {21, 22 , 28, 27, 39, 40, 46, 45}, {22, 23, 29, 28, 40, 41, 47, 46}, {23, 24, 30, 29, 41, 42, 48, 47}, {25, 26, 32, 31,43, 44, 50, 49} , {26, 27, 33, 32, 44, 45, 51, 50}, {27, 28, 34, 33, 45, 46, 52, 51}, {28, 29, 35, 34, 46, 47, 53, 52}, {29, 30, 36, 35, 47, 48, 54, 53}, /**{31, 32, 38, 37, 49, 50, 56, 55}, {32, 33, 39, 38,50, 51, 57, 56}, {33, 34, 40, 39, 51, 52, 58, 57}, { 34、35、41、40、52、53、59、 58}, {35, 36, 42, 41, 53, 54, 60, 59},**/ {37, 38, 44, 43,55, 56, 62, 61}, {38, 39, 45, 44 , 56, 57, 63, 62}, {39, 40, 46, 45, 57, 58, 64, 63}, {40, 41, 47, 46, 58, 59, 65, 64}, {41, 42, 48, 47,59, 60, 66, 65}, {43, 44、50、49、61、62、68、 67}、{44、45、51、50、62、63、69、68}、{45、46、52、51、63、64、70、69}、{46、47、53、52、64、 65, 71, 70}, {47, 48, 54, 53, 65, 66, 72, 71}, /**{49, 50, 56, 55, 67, 68, 74, 73}, {50, 51, 57, 56, 68, 69, 75, 74}, {51,52, 58, 57, 69, 70, 76, 75}、{52、53、59、58、70、71、77、76}、{53、54、60、59、71、72、78、77}}**/

现在,如果你观察到,我想要前十个元素 (modifiedtable 的第 1 到第 10 个元素)出现在我的最终表中 (DoubleModifiedTable)。接下来的五个(modifiedtable 的第 11 到 15 个元素)被删除。 然后是接下来的十个元素(modifiedtable 的第 16 到 25 个元素) 出现在我的决赛桌中 ( DoubleModifiedTable ) 然后删除接下来的 5 个元素(modifiedtable 的第 26 到 30 个元素),整个表依此类推。

假设我们解决了这个问题,并将最终表命名为 DoubleModifiedTable

我基本上对获取 DoubleModifiedTable 感兴趣。我决定将问题细分,因为它很容易解释。

我希望这通过表自动发生,因为这只是一个示例表,但实际上我有一个巨大的表。如果我能理解如何解决这个表的这个问题,那么我也可以为我的大表解决这个问题。

I have a question which can be divided into two subquestions.
I have created a table the code of which is given below.

Problem 1.

xstep = 1;
xmaximum = 6;
numberofxnodes = 6;
numberofynodes = 3;
numberofzlayers = 3;
maximumgridnodes = numberofxnodes*numberofynodes
mnodes = numberofxnodes*numberofynodes*numberofzlayers;
orginaltable = 
   Table[{i, 
          node2 = i + xstep, node3 = node2 + xmaximum,
          node4 = node3 - xstep,node5 = i + maximumgridnodes,
          node6 = node5 + xstep,node7 = node6 + xmaximum,
          node8 = node7 - xstep}, 
        {i, 1, mnodes}]

If I run this I will get my original table. Basically I want to remove the sixth element and multiples of the sixth element from my original table. I am able to do this by using this code below.

 modifiedtable = Drop[orginaltable, {6, mnodes, 6}]

Now I get the modified table where every sixth element and multiples of sixth element of my original table is removed. This solves my Problem 1.

Now my Problem 2:

** MAJOR EDITED VERSION**:(ALL THE CODES GIVEN ABOVE IS CORRECT)

Thanks a lot for the answers, but I wanted something else and I made a mistake
while explaining it initially so I'm making another try.

Below is my modified table: I want the elements in between
"/** and **/" deleted and remaining there.

{{1, 2, 8, 7, 19, 20, 26, 25}, {2, 3, 9, 8, 20, 21, 27, 26}, {3, 4,10, 9, 21, 22, 28, 27}, {4, 5, 11, 10, 22, 23, 29, 28}, {5, 6, 12, 11, 23, 24, 30, 29}, {7, 8, 14, 13, 25, 26, 32, 31}, {8, 9, 15, 14, 26, 27, 33, 32}, {9, 10, 16, 15, 27, 28, 34, 33}, {10, 11, 17, 16, 28, 29, 35, 34}, {11, 12, 18, 17, 29, 30, 36, 35}, /**{13, 14, 20, 19, 31, 32, 38, 37}, {14, 15, 21, 20, 32, 33, 39, 38}, {15, 16, 22, 21, 33, 34, 40, 39}, {16, 17, 23, 22, 34, 35, 41, 40}, {17, 18, 24, 23, 35, 36, 42, 41},**/ {19, 20, 26, 25, 37, 38, 44, 43}, {20, 21, 27, 26, 38, 39, 45, 44}, {21, 22, 28, 27, 39, 40, 46, 45}, {22, 23, 29, 28, 40, 41, 47, 46}, {23, 24, 30, 29, 41, 42, 48, 47}, {25, 26, 32, 31,43, 44, 50, 49}, {26, 27, 33, 32, 44, 45, 51, 50}, {27, 28, 34, 33, 45, 46, 52, 51}, {28, 29, 35, 34, 46, 47, 53, 52}, {29, 30, 36, 35, 47, 48, 54, 53}, /**{31, 32, 38, 37, 49, 50, 56, 55}, {32, 33, 39, 38,50, 51, 57, 56}, {33, 34, 40, 39, 51, 52, 58, 57}, {34, 35, 41, 40, 52, 53, 59, 58}, {35, 36, 42, 41, 53, 54, 60, 59},**/ {37, 38, 44, 43,55, 56, 62, 61}, {38, 39, 45, 44, 56, 57, 63, 62}, {39, 40, 46, 45, 57, 58, 64, 63}, {40, 41, 47, 46, 58, 59, 65, 64}, {41, 42, 48, 47,59, 60, 66, 65}, {43, 44, 50, 49, 61, 62, 68, 67}, {44, 45, 51, 50, 62, 63, 69, 68}, {45, 46, 52, 51, 63, 64, 70, 69}, {46, 47, 53, 52, 64, 65, 71, 70}, {47, 48, 54, 53, 65, 66, 72, 71}, /**{49, 50, 56, 55, 67, 68, 74, 73}, {50, 51, 57, 56, 68, 69, 75, 74},{51,52, 58, 57, 69, 70, 76, 75}, {52, 53, 59, 58, 70, 71, 77, 76}, {53, 54, 60, 59, 71, 72, 78, 77}}**/

Now, if you observe, I wanted the first ten elements
(1st to 10th element of modifiedtable) to be there in my final table
( DoubleModifiedTable ). the the next five (11th to 15th elements of modifiedtable) deleted.
Then the next ten elements ( 16th to 25th elements of modifiedtable)
to be present in my final table ( DoubleModifiedTable )
then the next five deleted (26th to 30th elements of modifiedtable) and so on for the whole table.

Let say we solve this problem and we name the final table DoubleModifiedTable.

I am basically interested in getting the DoubleModifiedTable. I decided to subdivide the problem as it easy to explain.

I want this to happen automatically through the table since as this is just an example table but in reality I have huge table. If I can understand how I can solve this problem for this table, then I can solve it for my large table too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懵少女 2024-12-09 20:41:59

也许更简单:

DoubleModifiedTable = 
  Module[{copy  = modifiedtable},
  copy[[Flatten[# + Range[5] & /@ Range[10, Length[copy], 10]]]] = Sequence[];
  copy]

编辑

甚至更简单:

DoubleModifiedTable =
   Delete[modifiedtable,
     Transpose[{Flatten[# + Range[5] & /@ Range[10, Length[modifiedtable], 10]]}]]

编辑2

根据OP的请求:只需在我的任何解决方案中更改一个数字(10到15)即可获得答案修改后的问题:

DoubleModifiedTable =
  Delete[modifiedtable,
    Transpose[{Flatten[# + Range[5] & /@ Range[10, Length[modifiedtable], 15]]}]]

Perhaps simpler:

DoubleModifiedTable = 
  Module[{copy  = modifiedtable},
  copy[[Flatten[# + Range[5] & /@ Range[10, Length[copy], 10]]]] = Sequence[];
  copy]

EDIT

Even simpler:

DoubleModifiedTable =
   Delete[modifiedtable,
     Transpose[{Flatten[# + Range[5] & /@ Range[10, Length[modifiedtable], 10]]}]]

EDIT 2

Per OP's request: one only has to change a single number (10 to 15) in any of my solutions to get the answer to a modified problem:

DoubleModifiedTable =
  Delete[modifiedtable,
    Transpose[{Flatten[# + Range[5] & /@ Range[10, Length[modifiedtable], 15]]}]]
风吹雨成花 2024-12-09 20:41:59

另一种方法是执行诸如

DoubleModifiedTable = With[{n = 10, m = 5},
  Flatten[{{modifiedtable[[;; m]]}, 
    Partition[modifiedtable, n - m, n, {n - m + 1, 1}, {}]}, 2]]

编辑

之类的操作。问题2的编辑版本实际上比原始版本更容易解决。例如,您可以执行类似

DoubleModifiedTable = 
 With[{n = 10, m = 5}, Flatten[Partition[modifiedtable, n, n + m, 1, {}], 1]] 

Edit 2

的操作,我的第二个版本所做的是使用 Partition 将原始列表 modifiedtable 拆分为子列表,然后进行展平这些子列表形成最终列表。如果您查看分区文档,您会发现可以看到我使用的是 Partition 的第六种形式,这意味着子列表的长度是 n ,偏移量(距离是n+m 因此,子列表之间的间隙为 n+mn==m

下一个参数 1 实际上相当于 < code>{1,1} 告诉 Mathematica modifiedtable 的第一个元素应出现在第一个子列表中的位置 1,而 modifiedtable 的最后一个元素应出现在出现在最后一个子列表的位置 1 上或之后。

最后一个参数 {} 表示长度为 <=n 的子列表不应使用填充。

总之,如果要删除前 10 个元素。并保留接下来的 5 个长度为 n=5 且间隙为 m=10 的子列表,因为您希望第一个子列表以 (m+1) 开头。 )-第一个元素modifiedtable,您可以将 Partition 中的第四个参数替换为 {k,1} 形式,以获得 k< 的某个值/code> 但提前删除 modifiedtable 的前 m 元素可能更容易,即

DoubleModifiedTable = 
 With[{n = 5, m = 10}, 
  Flatten[Partition[Drop[modifiedtable, m], n, n + m, 1, {}], 1]]

Another way is to do something like

DoubleModifiedTable = With[{n = 10, m = 5},
  Flatten[{{modifiedtable[[;; m]]}, 
    Partition[modifiedtable, n - m, n, {n - m + 1, 1}, {}]}, 2]]

Edit

The edited version of Problem 2 is actually slightly simpler to solve than the original version. You could for example do something like

DoubleModifiedTable = 
 With[{n = 10, m = 5}, Flatten[Partition[modifiedtable, n, n + m, 1, {}], 1]] 

Edit 2

What my second version does is to split the original list modifiedtable into sublists using Partition and then to flatten these sublists to form the final list. If you look at the Documentation for Partition you can see that I'm using the 6th form of Partition which means that the length of the sublists is n and the offset (the distance be is n+m. The gap between the sublists is therefore n+m-n==m.

The next argument, 1, is actually equivalent to {1,1} which tells Mathematica that the first element of modifiedtable should appear at position 1 in the first sublist and the last element of modifiedtable should appear on or after position 1 of the last sublist.

The last argument, {} is to indicate that no padding should be used for sublists with length <=n.

In summary, if you want to delete the first 10 elements and keep the next 5 you want sublists of length n=5 with gap m=10. Since you want the first sublist to start with the (m+1)-th element of modifiedtable, you could replace the fourth argument in Partition with something of the form {k,1} for some value of k but it's probably easier to just drop the first m elements of modifiedtable beforehand, i.e.

DoubleModifiedTable = 
 With[{n = 5, m = 10}, 
  Flatten[Partition[Drop[modifiedtable, m], n, n + m, 1, {}], 1]]
撧情箌佬 2024-12-09 20:41:59
DoubleModifiedTable=
modifiedtable[[
  Complement[
   Range[Length[modifiedtable]],
   Flatten@Table[10 i + j, {i, Floor[Length[modifiedtable]/10]}, {j, 5}]
  ]
]]

或者,稍微短一些

DoubleModifiedTable=
#[[
    Complement[
       Range[Length[#]],
       Flatten@Table[10 i + j, {i, Floor[Length[#]/10]}, {j, 5}]
     ]
 ]] & @ modifiedtable
DoubleModifiedTable=
modifiedtable[[
  Complement[
   Range[Length[modifiedtable]],
   Flatten@Table[10 i + j, {i, Floor[Length[modifiedtable]/10]}, {j, 5}]
  ]
]]

or, slightly shorter

DoubleModifiedTable=
#[[
    Complement[
       Range[Length[#]],
       Flatten@Table[10 i + j, {i, Floor[Length[#]/10]}, {j, 5}]
     ]
 ]] & @ modifiedtable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文