2

I have the data in the following structures:

Nums←56 66 19 37 44 20 18 23 68 70 82

A←(⍳¨(3≤⍳4)/⍳4)
┌→────┬───────┐
│1 2 3│1 2 3 4│
└~───→┴~─────→┘

What I want to do is create another nested array that would have elements with values from Nums, selected using indexes from A, looking like that:

┌→───────┬───────────┐
│56 66 19│56 66 19 37│
└~──────→┴~─────────→┘

Then I want to perform operations on every element of my array but I think I know how to go from there. How do I create such array in APL?

syntagma
  • 23,346
  • 16
  • 78
  • 134

3 Answers3

1

How about

(⊂Nums){⍺[⍵]}¨A

and you can then go ahead and appy youf fn:

(⊂Nums){fn ⍺[⍵]}¨A
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Thanks. It does what I need but I'm now wondering whether there is a better way to do what I'm trying to do (which is: apply same operation on the selected segments of the vector, all segments having the same length). – syntagma May 07 '13 at 06:34
  • In your example you have two segments with different lengths. How would you now control the length? One answer might be to apply the fn at segments of length 4: (⊂Nums){fn 4↑⍺[⍵]}¨A (nb: a 0 would be appended to the vector of length 3...) – MBaas May 07 '13 at 14:56
0

Here's another way to structure the result

      A⌷¨¨⊂⊂Nums
56 66 19  56 66 19 37
Bob Smith
  • 125
  • 1
  • 6
0

In NARS2000, easy:

  Nums←56 66 19 37 44 20 18 23 68 70 82
  A←(⍳3)(⍳4)     
  ⎕fmt A
┌2──────────────────┐
│┌3─────┐ ┌4───────┐│
││ 1 2 3│ │ 1 2 3 4││
│└~─────┘ └~───────┘2
└∊──────────────────┘
  ⎕fmt {Nums[⍵]}¨¨A
┌2─────────────────────────┐
│┌3────────┐ ┌4───────────┐│
││ 56 66 19│ │ 56 66 19 37││
│└~────────┘ └~───────────┘2
└∊─────────────────────────┘
RosLuP
  • 153
  • 7