Combining vectors in APL (creating tuples with elements from each vector)
Question
I have 2 vectors:
a ← 1 2 3
b ← 5 6 7
I would like to create a nested array with 2-element vector elements, having one element from a b (tuples), i.e.:
┌───┬───┬───┐
│1 5│2 6│3 7│
└───┴───┴───┘
How would I do that in APL? I can't find any elastic solution, only (a[1],b[1]) (a[2],b[2])
(I've also tried Outer product with the ⊃
function but without success).
Solution
Try this:
⊂[2] a,[1.5]b
The first part: a,[1.5]b
merges the two arrays and creates a two-dimensional array with each column being the content of the variables a and b.
Then, ⊂[2]
encapsulates each row into pairs, resulting in your desired output.
Note that if you are using Dyalog, you might have to use ↑ instead of ⊂. I think this depends on the value of the ⎕ML variable, but I don't use Dyalog so I can't test this.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow