Cells

Experiment with the following examples. Describe each one and then describe the behaviour of the rank operator in terms of and . Do not be discouraged by longer expressions and unfamiliar symbols. To help understanding, break down the expression and try pieces of it at a time.

      names←↑'Angela' 'Pete' 'Leslie'   ⍝ A matrix of names padded with spaces
      scores←3 6 8
      'Pete  '(=⍤1 1)names
      scores[names⍳'Pete  ']
      (∧/names(=⍤1 1)'Pete  ')⌿scores
      names(∨/⍷⍤1)(⊃⌽⍴names)↑'Pete'
      mass←1 3 5 8 4
      pos←5 3⍴0 1 3 4 2
      {(+⌿⍵)÷≢⍵}mass(×⍤0 2)pos
      ×⍤0 2⍨⍳10      
When applying dyadic functions using the rank operator, use the helper function ,⍥⊂ ravel over enclose (or {⍺⍵} for versions before Dyalog version 18.0) to see how arguments are paired up. For example:
      names(,⍥⊂⍤1 1)'Pete  '
      ⍉pos,⍥⊂⍤2 0⊢mass
If you still feel stuck, check out The Array Model.

Rank

Which of the following functions are affected by the rank operator , and why are the other functions not affected?

      ⌽    ⍝ Reverse
      ⊖    ⍝ Reverse first
      +/   ⍝ Plus reduce
      +⌿   ⍝ Plus reduce-first

The Shape of the Result

Without executing them, determine the shape of the results of the following expressions.

      1 3 5∘.!2 4 6 8           
      1 2 3 + 4 5 6             
      {(+⌿⍵)÷≢⍵}3 1 4 1 5       
      +⌿2 3⍴⍳6                  
      ?⌿2 3⍴3/4 52              
      (⌈⌿⍤2)(2 3⍴⍳6)∘.ׯ1+?3 4⍴0 
Of course, you can always check your answers using the shape function

Common Names for Arrays of Rank-n

  1. Match the following rank operands with their descriptions. Each use of rank pairs with two of the 10 description boxes below.

       1    2    3    4     5
     ┌────┬────┬───┬─────┬──────┐
     │⍤1 3│⍤2 1│⍤¯1│⍤0 99│⍤99 ¯1│
     └────┴────┴───┴─────┴──────┘
    -----------------------------------------
     ┌─┐ ┌────────────────┐ ┌────────────┐
     │⍵│ │major cells of ⍺│ │vectors of ⍺│
     └─┘ └────────────────┘ └────────────┘
     ┌────────────────┐ ┌─┐ ┌──────────────┐
     │major cells of ⍵│ │⍺│ │3D arrays of ⍵│
     └────────────────┘ └─┘ └──────────────┘
     ┌────────────────┐ ┌────────────┐
     │major cells of ⍵│ │scalars of ⍺│
     └────────────────┘ └────────────┘
     ┌────────────────┐ ┌────────────────┐
     │matrices of ⍺   │ │vectors of ⍵    │
     └────────────────┘ └────────────────┘
    
  2. For each name below, suggest the rank for arrays with that name.

     ┌────────┬────────────────────┐
     │Scalar  │                    │
     ├────────┼────────────────────┤
     │Vector  │rank-1 array        │
     ├────────┼────────────────────┤
     │Matrix  │                    │
     ├────────┼────────────────────┤
     │Table   │                    │
     ├────────┼────────────────────┤
     │List    │                    │
     ├────────┼────────────────────┤
     │Cube    │                    │
     ├────────┼────────────────────┤
     │4D array│                    │
     ├────────┼────────────────────┤
     │2D array│                    │
     └────────┴────────────────────┘
    

Some Points in Space Revisited

These problems are identical to those about Some Points in Space in the previous section. This time, create a function which works on vectors and use the rank operator to solve these problems.

The positions of 7 points in 2D space are given by the matrix pos2←7 2⍴0 1 3 4 2 7 3.

  1. Write a function NormVec to normalise a vector so that its sum of squares is 1.

           +/pos2*2
     1 25 53 9 10 20 58
           +/((NormVec⍤1)pos2)*2
     1 1 1 1 1 1 1
           ÷/2-/pos2
     ¯1 ¯1 ¯5 3 ¯2 2 4
           ÷/2-/(NormVec⍤1)pos   ⍝ Relative proportions stay the same
     ¯1 ¯1 ¯5 3 ¯2 2 4  
    
  2. Find the values of j and k in each of the two expressions below.

    1.        0 10(×⍤j k)pos2
       0 10
       0 40
       0 70
       0  0
       0 30
       0 20
       0 30
      
    2.        (2×⍳7)(×⍤j k)pos2
        2  3
        7  8
        8 13
       11  8
       11 13
       16 14
       21 17
      

Rank Matching

Write a function R1 which uses catenate , with the rank operator to merge a vector and matrix into a single 3D array.

      'ABC' R1 2 3⍴⍳6
1 A
2 B
3 C
   
4 A
5 B
6 C
Hint:

You can apply rank multiple times e.g. f⍤j⍤k.