自动删除 Mathematica 表中的特定元素
我有一个问题可以分为两个子问题。 我创建了一个表,其代码如下。
问题 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许更简单:
编辑
甚至更简单:
编辑2
根据OP的请求:只需在我的任何解决方案中更改一个数字(10到15)即可获得答案修改后的问题:
Perhaps simpler:
EDIT
Even simpler:
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:
另一种方法是执行诸如
编辑
之类的操作。问题2的编辑版本实际上比原始版本更容易解决。例如,您可以执行类似
Edit 2
的操作,我的第二个版本所做的是使用
Partition
将原始列表modifiedtable
拆分为子列表,然后进行展平这些子列表形成最终列表。如果您查看分区文档,您会发现可以看到我使用的是Partition
的第六种形式,这意味着子列表的长度是n
,偏移量(距离是n+m
因此,子列表之间的间隙为n+mn==m
。下一个参数
1
实际上相当于 < code>{1,1} 告诉 Mathematicamodifiedtable
的第一个元素应出现在第一个子列表中的位置 1,而modifiedtable
的最后一个元素应出现在出现在最后一个子列表的位置 1 上或之后。最后一个参数
{}
表示长度为<=n
的子列表不应使用填充。总之,如果要删除前 10 个元素。并保留接下来的 5 个长度为
n=5
且间隙为m=10
的子列表,因为您希望第一个子列表以(m+1) 开头。 )
-第一个元素modifiedtable
,您可以将Partition
中的第四个参数替换为{k,1}
形式,以获得k< 的某个值/code> 但提前删除
modifiedtable
的前m
元素可能更容易,即Another way is to do something like
Edit
The edited version of Problem 2 is actually slightly simpler to solve than the original version. You could for example do something like
Edit 2
What my second version does is to split the original list
modifiedtable
into sublists usingPartition
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 ofPartition
which means that the length of the sublists isn
and the offset (the distance be isn+m
. The gap between the sublists is thereforen+m-n==m
.The next argument,
1
, is actually equivalent to{1,1}
which tells Mathematica that the first element ofmodifiedtable
should appear at position 1 in the first sublist and the last element ofmodifiedtable
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 gapm=10
. Since you want the first sublist to start with the(m+1)
-th element ofmodifiedtable
, you could replace the fourth argument inPartition
with something of the form{k,1}
for some value ofk
but it's probably easier to just drop the firstm
elements ofmodifiedtable
beforehand, i.e.或者,稍微短一些
or, slightly shorter