Indices: Difference between revisions

From NARS2000
Jump to navigationJump to search
(Created page with "<table border="1" cellpadding="5" cellspacing="0" rules="none" summary=""> <tr> <td> <table border="0" cellpadding="5" cellspacing="0" summary=""> <tr> <td valign…")
 
No edit summary
Line 7: Line 7:
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td>returns an integer vector identical to <apll>R/⍳&gt;R</apll>.</td>
       <td>returns an integer vector or nested vector of integer vectors identical to <apll>⊃,/,R/¨⊂¨⍳⍴1/R</apll>.</td>
     </tr>
     </tr>
     </table>
     </table>
Line 13: Line 13:
</tr>
</tr>
<tr>
<tr>
   <td><apll>R</apll> is a scalar or vector of non-negative integers.</td>
   <td><apll>R</apll> is an array of non-negative integers.</td>
</tr>
</tr>
<tr>
<tr>
   <td><apll>Z</apll> is an integer vector of length <apll>+/R</apll>whose values are <apll>R/⍳&gt;R</apll> where monadic <apll>&gt;</apll> is the [[Tally]] function.</td>
   <td><apll>Z</apll> is an integer vector of length <apll>+/,R</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>This feature encapsulates a very common idiom in one symbol.</td>
   <td>For scalars or vectors, the result is equivalent to <apll>R/⍳⍴1/R</apll> which encapsulates a very common idiom in one symbol.</td>
</tr>
<tr>
  <td>For higher rank arrays, the result extends to produce a nested vector of vectors of the indices of all the positive integer elements of <apll>R</apll> replicated as per the corresponding value in <apll>R</apll>.</td>
</tr>
</tr>
</table>
</table>
Line 28: Line 31:
0 2 3 4 6<br />
0 2 3 4 6<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸2 3 4<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸2 3 4<br />
0 0 1 1 1 2 2 2 2</apll>
0 0 1 1 1 2 2 2 2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸⎕←2 3⍴⍳4<br />
&nbsp;0 1 2<br />
&nbsp;3 0 1<br />
&nbsp;0 1&nbsp; 0 2&nbsp; 0 2&nbsp; 1 0&nbsp; 1 0&nbsp; 1 0&nbsp; 1 2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸1 2 3⍴⍳4<br />
&nbsp;0 0 1&nbsp; 0 0 2&nbsp; 0 0 2&nbsp; 0 1 0&nbsp; 0 1 0&nbsp; 0 1 0&nbsp; 0 1 2<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←(Txt Rep) txtrep Z;a<br />
[1]&nbsp;&nbsp;&nbsp;⍝ Replace Txt in Z with Rep.<br />
[2]&nbsp;&nbsp;&nbsp;:Assert 2=⍴⍴Z ⋄ :Assert (⍴Txt)≡⍴Rep<br />
[3]&nbsp;&nbsp;&nbsp;a←⍸Txt⍷Z<br />
[4]&nbsp;&nbsp;&nbsp;Z[⊃⊃¨,¨/¨a+⊂0(0..¯1+⍴Txt)]←((⍴a),⍴Rep)⍴Rep<br />
&nbsp;&nbsp;&nbsp;&nbsp;∇<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Now' 'Who' txtrep 4 13⍴'Now is the time...'<br />
Who is the ti<br />
me...Who is t<br />
he time...Who<br />
&nbsp;is the time.</apll>

Revision as of 12:34, 30 October 2012

Z←⍸R returns an integer vector or nested vector of integer vectors identical to ⊃,/,R/¨⊂¨⍳⍴1/R.
R is an 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

      ⍸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.