Skip to content

Interval Index R←X⍸Y

Classic Edition

The symbol (Iota Underbar) is not available in Classic Edition, and Interval Index is instead represented by ⎕U2378.

X is an ordered non-scalar array that represents a set of intervals or ranges.

Note that the ith interval starts at X[i], then includes all subsequent values up to but not including X[i+1].

For example, if X is (1 3 5) it defines 4 intervals numbered 0 to 3 as follows.

0 less than 1 <1
1 between 1 and 3 (≥1)∧(<3)
2 between 3 and 5 (≥3)∧(<5)
3 greater than or equal to 5 ≥5

If X is 'AEIOU' it defines 6 intervals numbered 0 to 5 as follows:

0 before A ⎕UCS 0,⍳64
1 between A and E ABCD
2 between E and I EFGH
3 between I and O IJKLMN
4 between O and U OPQREST
5 U and after UVWXYZ...

Y is an array of the same type (numeric or character) as X.

The result R is an integer array that identifies into which interval the corresponding value in Y falls.

Like dyadic (see Index Of), Interval Index works with major cells. For a vector these are its elements; for a matrix its rows, and so forth.

X and Y are compared using the same logic as monadic (see Grade Up (Monadic)) which is independent of ⎕CT and ⎕DCT.

⎕IO is an implicit argument of Interval Index. In all the following examples, ⎕IO is 1.

Example

      10 20 3011 1 31 21
1 0 3 2

In the above example:

  • 11 is between X[1] and X[2] so the answer is 1.
  • 1 is less than X[1] so the answer is 0
  • 31 is greater than X[⍴X] so the answer is 3
  • 21 is between X[2] and X[3] so the answer is 2.
          'AEIOU'  'DYALOG'
    1 5 1 3 4 2
    

And in the alphabetic example above:

  • "D" is between X[1] and X[2], so the answer is 1
  • "Y" is after X[⍴X] so the answer is 5
  • "A" is between X[1] and X[2], so the answer is 1
  • as so on ...

Example (Classification)

Commercially, olive oil is graded as follows:

  • if its acidity is less than 0.8%, as "Extra Virgin"
  • if its acidity is less than 2%, as "Virgin"
  • if its acidity is less than 3.3%, as "Ordinary"
  • otherwise, as "Lampante"
         grades'Extra Virgin' 'Virgin' 'Ordinary' 'Lampante'
         acidity0.8 2 3.3
    
         samples1.3 1.9 0.7 4 .6 3.2
         aciditysamples
    1 1 0 3 0 2
         samples,⍪grades[1+aciditysamples]
    ┌───┬────────────┐
    1.3Virgin      
    ├───┼────────────┤
    1.9Virgin      
    ├───┼────────────┤
    0.7Extra Virgin
    ├───┼────────────┤
    4  Lampante    
    ├───┼────────────┤
    0.6Extra Virgin
    ├───┼────────────┤
    3.2Ordinary    
    └───┴────────────┘
    

Example (Data Consolidation by Interval)

x represents some data sampled in chronological order at timestamps t.

      x
200000
      x
3984300 2020650 819000 1677100 3959200 2177250 3431800 ...
      t
200000 3
      (10t) (¯10t)
┌─────┬────────┐
0 0 023 59 54
0 0 023 59 55
0 0 023 59 56
0 0 023 59 56
0 0 023 59 58
0 0 223 59 58
0 0 323 59 59
0 0 323 59 59
0 0 423 59 59
0 0 523 59 59
└─────┴────────┘

u represents timestamps for 5-minute intervals:

      u
288 3
      (10u) (¯10u)
┌──────┬───────┐
0  0 023 10 0
0  5 023 15 0
0 10 023 20 0
0 15 023 25 0
0 20 023 30 0
0 25 023 35 0
0 30 023 40 0
0 35 023 45 0
0 40 023 50 0
0 45 023 55 0
└──────┴───────┘

Therefore, the expression (u⍸t){+/⍵}⌸x summarises x in 5-minute intervals.

      u  t
1 1 1 1 1 1 1 1 1 1 ... 288 288 288 288 288 288

      (ut) {+/} x
1339083050 1365108650 1541944750 1393476000 1454347100 ...

      (ut) {(u),+/} x
 0  0 0 1339083050
 0  5 0 1365108650
 0 10 0 1541944750
 0 15 0 1393476000
   ...
23 45 0 1388823150
23 50 0 1453472350
23 55 0 1492078850

Higher-Rank Left Argument

If X is a higher rank array, the function compares sub-arrays in Y with the major cells of X, where a major cell is a sub-array on the leading dimension of X with shape 1↓⍴X. In this case, the shape of the result R is (1-⍴⍴X)↓⍴Y.

Example

      x   'Fi' 'Jay' 'John' 'Morten' 'Roger'
      x
Fi
Jay   
John  
Morten
Roger 
      x
5 6
      y  x   'JD' 'Jd' 'Geoff' 'Alpha' 'Omega' 'Zeus  '
      y
Fi
Jay   
John  
Morten
Roger 
JD    
Jd    
Geoff 
Alpha 
Omega 
Zeus
      x  y
1 2 3 4 5 1 2 1 0 4 5
      y ,⍪ xy
Fi     1
Jay    2
John   3
Morten 4
Roger  5
JD     1
Jd     2
Geoff  1
Alpha  0
Omega  4
Zeus   5

Further Example

      x
5 6
      y
3 3 6
      x
Fi    
Jay   
John  
Morten
Roger 
      y
Fi    
Jay   
John  

Morten
Roger 
JD    

Jd    
Geoff 
Alpha 
      xy
1 2 3
4 5 1
2 1 0

Nested Array Example

A card-player likes to sort a hand into suits spades, hearts, diamond, clubs (fortunately alphabetic) and high-to-low within each suit.

      suits'Clubs' 'Diamonds' 'Hearts' 'Spades'
      pack,(¨suits)∘.,114 ⍝ 11=Jack ... 14=Ace
      hand(,pack)[7?52]
      handhand[hand;]
      hand
┌────────┬──┐
Spades  12
├────────┼──┤
Hearts  12
├────────┼──┤
Hearts  7 
├────────┼──┤
Hearts  2 
├────────┼──┤
Diamonds11
├────────┼──┤
Diamonds9 
├────────┼──┤
Clubs   8 
└────────┴──┘

Another card, the 10 of diamonds is dealt. Where must it go in the hand ?

      (hand)'Diamonds' 10 ⍝ left arg must be sorted up
2
      (¯2hand)'Diamonds' 10¯2hand
┌────────┬──┐
Spades  12
├────────┼──┤
Hearts  12
├────────┼──┤
Hearts  7 
├────────┼──┤
Hearts  2 
├────────┼──┤
Diamonds11
├────────┼──┤
Diamonds10
├────────┼──┤
Diamonds9 
├────────┼──┤
Clubs   8 
└────────┴──┘

Note that if (∧/Y∊X) and X is sorted and ⎕CT=0 ,then x⍸y is the same as x⍳y.