Skip to content

Labelling

Vector 8:1, page 30

education programming techniques

Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 30

In a previous Education Vector I looked at one way of tackling the problem of adding column and row titles to a matrix. Recently I have up-dated my method of approach so that it was more automatic, but without the facility to right- or left-justify the column titles.

The programs do have their use in the production of educational material so I have taken the liberty to include them in this issue and also use them in my article.

I guess that many professional APL programmers have their own such utility functions which may or may not be easily available to the general public. I shall be delighted if the professional programmers who read Education Vector throw up their hands in horror at what I produce here, as long as they write to me with their preferred solution so that it too can be made available in a future edition of Education Vector!

The programs are RLABEL, CLABEL, and DTB. To input titles as left arguments, use a separator to create a character string, such as ‘label 1/label 2/’. The function DTB provides an option of changing a character matrix of labels into such a string, deleting the trailing blanks in the process.

One technical problem with my approach is that I wish to be able to detect blank columns of the array automatically. However, the row titles themselves could contain a blank column. Hence I have used a character from ⎕AV (⎕AV[256] for APLPLUS), which is used to separate the row labels from the rest of the table. Here are the programs and a short demonstration of their use.

    ∇ R←ROWLABELS RLABEL TAB;T
[1]   ⍝ computes the right argument for CLABEL
[2]   ⍝ with rowlabs and formatted num table separated by ⎕AV[256]
[3]   ⍝ left argument either a matrix or a vector with separators
[4]    ⍎(1=⍴⍴ROWLABELS)/'ROWLABELS←BOX ROWLABELS'
[5]    R←ROWLABELS,⎕AV[256],(~∧\∧⌿T=' ')/T←⍕TAB
[6]    ⍎(0=∧/R[;(⍴R)[2]]=' ')/'R←R,'' '''
    ∇

    ∇ R←COLLAB CLABEL TABLE;I;J;II;JJ;M;N;MM;NN;TI;WD
[1]   ⍝ Puts column labels on the top of table
[2]   ⍝ left arg is a set of labels either as string or matrix
[3]    ⍎(1=⍴⍴TABLE)/'TABLE←(1,⍴TABLE)⍴TABLE'
[4]    ⍎(0=∧/TABLE[;(⍴TABLE)[2]]=' ')/'TABLE←TABLE,'' '''
[5]    ⍎(2=⍴⍴COLLAB)/'COLLAB←DTB COLLAB'
[6]    TABLE←(~∧\∧⌿TABLE=' ')/TABLE
[7]    I←(∧⌿TABLE=' ') ⋄ TI←(∧⌿TABLE=⎕AV[256])⍳1 ⋄ →(TI>⍴I)/cont
[8]    I[⍳¯1+TI]←0 ⋄ I[TI+ 0 1]← 1 0
[9]   cont:J←I=1⌽I ⋄ I[J/⍳⍴I]←0 ⋄ I[⍴I]←1
[10]   II←I/⍳⍴I ⋄ M←II-0,¯1↓II
[11]   TABLE[;II[1]]←' '
[12]   J←COLLAB=¯1↑COLLAB ⋄ JJ←J/⍳⍴J ⋄ N←JJ-0,¯1↓JJ
[13]   COLLAB←COLLAB,[0.5]'-' ⋄ COLLAB[;JJ]←' '
[14]   I[II]←I[II]+3+(M⌈N)-M ⋄ J[JJ]←J[JJ]+3+(M⌈N)-N
[15]   I←I+1 ⋄ J←J+1 ⋄ WD←+/I
[16]   R←(2,WD)↑J/COLLAB ⋄ R[;(R[1;]=' ')/⍳¯1↑⍴R]←' '
[17]   R←R,[1]((1↑⍴TABLE),WD)↑I/TABLE
[18]   R←(⌽∨\⌽~∧⌿R=' ')/R
    ∇

    ∇ L←BV BOX X;I;J;K;REP;M;F
[1]   ⍝ converts segmented string to character matrix
[2]    →(F←0≠⎕NC 'BV')/ct ⋄ BV←¯1↑X
[3]   ct:X←X,F⍴BV ⋄ L←(1,⍴,X)⍴X ⋄ →(~∨/I←BV=,X)/0
[4]    I←I/⍳⍴I ⋄ X[I]←' ' ⋄ J←¯1+I-0,¯1↓I ⋄ K←(M←⌈/J)-J
[5]    REP←(⍴X)⍴1 ⋄ REP[I]←K ⋄ L←((⍴I),M)⍴REP/X
    ∇

    ∇ R←DTB MAT;T
[1]   ⍝ drops trailing blanks on MAT
[2]   ⍝ and returns a vector with separators /
[3]    T←⌽∧\⌽MAT=' '
[4]    R←(~,T,0)/,MAT,'/'
    ∇
      X←?3 4⍴10

      X
6 10
2 10
4  5
3  2
7  7
9 10

      'ROW 1/ROW 2/ROW 3/' RLABEL ⍕X
ROW 1 6 10  2 10
ROW 2 4  5  3  2
ROW 3 7  7  9 10

      'ROW/COL 1/COL 2/COL 3/COL 4/' CLABEL
          'ROW 1/ROW 2/ROW 3/' RLABEL ⍕X

ROW       COL 1     COL 2     COL 3     COL 4
---       --- -     --- -     --- -     --- -
ROW 1     6         10        2         10
ROW 2     4          5        3          2
ROW 3     7          7        9         10

      '/COL 1/COL 2/COL 3/COL4/' CLABEL
          'ROW 1/ROW 2/ROW 3/' RLABEL ⍕X

          COL 1     COL 2     COL 3     COL 4
          --- -     --- -     --- -     --- -
ROW 1     6         10        2         10
ROW 2     4          5        3          2
ROW 3     7          7        9         10

      MAT←3 5⍴'ALAN BILL PETER'
      MAT
ALAN
BILL
PETER

      DTB MAT
ALAN/BILL/PETER/

Readers may be interested to know that these programs are currently used to provide labelled output for several functions from the APL Statistics Library.