Problems when trying to use arrays in APL. What have I missed?

爷,独闯天下 提交于 2019-12-10 18:58:25

问题


I obviously have missed some things about how to extract elements from arrays in APL and hope that someone can see what I have missed and how I should do to get the expected results in a way that I can reproduce in a meaningful way.

I am relatively new in learning APL and I am more used to languages like Python and C. The data types and array manipulating tools in APL seem to confuse me, a little.

Consider the following code and please tell why the expected (by me) result,

┌→─────┐
│42 666│
└~─────┘

got embedded in something more complex, and possibly a way around that problem. (Using Dyalog APL/S-64, 16.0.30320)

      ⎕io ← 0
      a ← 17 4711 (42 666)
      z ← a[2]

      an_expected_vector←42 666
      ]DISPLAY an_expected_vector
┌→─────┐
│42 666│
└~─────┘

      ]DISPLAY z
┌──────────┐
│ ┌→─────┐ │
│ │42 666│ │
│ └~─────┘ │
└∊─────────┘

Why isn't z identical to an_expected_vector ?

Thanks ! /Hans


回答1:


2 is a scalar and so a[2] returns a scalar, which happens to be the vector 42 666. It is therefore enclosed in a level of nesting.

If you use the Pick function (dyadic ) you will get the expected result, as will pick the element indicated by the left argument, from the right argument:

       ⎕io ← 0
       a ← 17 4711 (42 666)
       z ← 2⊃a
       an_expected_vector ← 42 666
       z ≡ an_expected_vector
 1

Try it online!



来源:https://stackoverflow.com/questions/46821632/problems-when-trying-to-use-arrays-in-apl-what-have-i-missed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!