Indices

From NARS2000
Revision as of 16:31, 16 November 2012 by WikiSysop (talk | contribs)
Jump to navigationJump to search
Z←⍸R returns a simple integer vector or nested vector of integer vectors identical to (,R)/⍳⍴1/R.
R is a simple array of non-negative integers.
Z is an integer vector of length +/,R.
For scalars or vectors, the result is equivalent to R/⍳⍴1/R which encapsulates a very common idiom in one symbol.
For higher rank arrays, the result extends to produce a nested vector of vectors of the indices of all the positive integer elements of R replicated as per the corresponding value in R.


For example, in origin-0

      ⍸3
0 0 0
      ⍸1 0 1 1 1 0 1
0 2 3 4 6
      ⍸2 3 4
0 0 1 1 1 2 2 2 2
      ⍸⎕←2 3⍴⍳4
 0 1 2
 3 0 1
 0 1  0 2  0 2  1 0  1 0  1 0  1 2
      ⍸1 2 3⍴⍳4
 0 0 1  0 0 2  0 0 2  0 1 0  0 1 0  0 1 0  0 1 2

    ∇ Z←(Txt Rep) txtrep Z;a
[1]   ⍝ Replace Txt in Z with Rep.
[2]   :Assert 2=⍴⍴Z ⋄ :Assert (⍴Txt)≡⍴Rep
[3]   a←⍸Txt⍷Z
[4]   Z[⊃⊃¨,¨/¨a+⊂0(0..¯1+⍴Txt)]←((⍴a),⍴Rep)⍴Rep
    ∇
      'Now' 'Who' txtrep 4 13⍴'Now is the time...'
Who is the ti
me...Who is t
he time...Who
 is the time.