3

我是 APL 的新手。如何从数组中删除不需要的元素?

例如:

 X←1 2 3 4 5 6 7 8 9

现在我想删除等于 4 或 6 的 X 元素。我试过了

X←4↓X

从 X 中删除 4,但它没有用。提前致谢。

4

4 回答 4

11

The appropriate way to do this is using the without function (dyadic tilda):

      x←1 2 3 4 5 6 7 8 9
      x~4 6
1 2 3 5 7 8 9

However, if you need the location of the items you want removed for additional purposes (perhaps to remove the corresponding items from some some other related array) then the technique by MrZander above is appropriate.

于 2012-12-14T14:58:07.613 回答
7
于 2012-08-21T05:28:48.030 回答
0

You could create a binary mask such that 0 implies "element is 4 or 6" and 1 implies "element is neither 4 nor 6". Then you select from the array using this mask.

(~(6=X)∨(4=X))/X

于 2013-04-24T03:50:06.843 回答
0

For most purposes, the without function mentioned by Paul Mansour is a better approach. but if you wanted to use a bit-mask, try:

(~(X¹4 6))/X

(Note that "¹" is the "member" primitive, normally represented as an epsilon character.)

This selects all elements of X that are either 4 or 6, then applies a not to create a boolean that is 0 for all elements that are 4 or 6, and uses that to compress X, removing all 4s and 6s. Perhaps more useful would be:

((bv{gets}~X{member}4 6))/X

This would save the compression vector (aka mask) in a separate variable bv. If there are several structures that should be kept in sync, bv could then be used to compress the others to match X. Of course, a more complex condition for bv would be likely in real working code. ({gets} stands for the assignment operation, normally represented by a leftward arrow.)

于 2016-10-10T16:47:05.770 回答