1

I have the following data:

a b c d
5 9 6 0
3 1 3 2

Characters in the first row, numbers in the 2nd and 3rd rows.

How can I change the second row depending on the numbers in row 3?

For example, in the 3rd row, if the number is 3, then the corresponding number in row 2 is multiplied by 2, and if in the 3rd row the number is 1, then add 3 to the number in row 2:

a  b  c  d
10 12 12 0
3  1  3  2

Also, how do I dynamically calculate the sum of these increases?

Dyalog APL 17.0+

Adám
  • 6,573
  • 20
  • 37
cickness
  • 737
  • 2
  • 6
  • 13
  • I have some questions 1. What if the number is neither 1 not 3 then what to do? 2. In output what you want? Updated second row or value by it's increased by? 3. What is 17+ mean here? – Abhay Sehgal Feb 23 '20 at 12:44
  • if number not 1 and not 3 need do nothing. In the output need just print upd row with changed numbers and not changed. and i want see number by which the second row increased –  cickness Feb 23 '20 at 12:50
  • 1
    @AbhaySehgal I've clarified that "17+" refers to [Dyalog APL 17.0](https://aplwiki.com/wiki/Dyalog_APL_versions#17.0) or newer. – Adám Feb 23 '20 at 12:59
  • 1
    @cickness You might be interested in frequenting [the Stack Exchange APL chat room](https://chat.stackexchange.com/rooms/52405/the-apl-orchard?_=1620076451), where you'll find both beginner and expert APLers. – Adám Feb 23 '20 at 13:02

2 Answers2

1

Create a multiplier vector and an addition vector from the condition masks.

n⌷data gets you the nth row a data matrix.

The columns of the second row that you want to double are indicated by:

      3=3⌷data
1 0 1 0

So the multiplication vector is:

      1+3=3⌷data
2 1 2 1

The columns of the second row that you want to add 3 to are:

      1=3⌷data
0 1 0 0

So the addition vector is:

      3×1=3⌷data
0 3 0 0

The new second row is thus:

      (3×1=3⌷data)+(1+3=3⌷data)×2⌷data
10 12 12 0

We can express this as a dyadic function taking the 3rd row as left argument (the control) and the second row as right argument (the actual data):

      Update←{(3×1=⍺)+(1+3=⍺)×⍵}
      (3⌷data) Update (2⌷data)
10 12 12 0

Now we can either create a new matrix with the updated values:

      (3⌷data) Update@2 ⊢data
 a  b  c d
10 12 12 0
 3  1  3 2

Or do the replacement in-place:

      (2⌷data) Update⍨← (3⌷data)
      data
 a  b  c d
10 12 12 0
 3  1  3 2

Try it online!


Note that your algorithms would be simplified and your code run faster if you kept data with different roles in separate variables. For example:

      (keys values control)←↓data
      control Update values
10 12 12 0
      values Update⍨← control
      values
10 12 12 0
      ↑keys values control
 a  b  c d
10 12 12 0
 3  1  3 2

Try it online!


The sum of the increases is simply the sum of the differences between the new values and the original values:

      +/values-⍨control Update values
14

Try it online!

Adám
  • 6,573
  • 20
  • 37
0

In NARS2000 (I don't know if is ok for other APL compilers too),
"⍵[2;],¨⍵[3;]" make couples of 2 rows of ⍵, the row n.2 and the row n.3.

  f←{(a b)←⍵⋄b=3:2×a⋄b=1:a+3⋄a}
  h←{3 4⍴⍵[1;],(f¨⍵[2;],¨⍵[3;]),⍵[3;]}
  B←3 4⍴'abcd',5 9 6 0 3 1 3 2
  ⎕fmt B
┌4───────┐
3 a b c d│
│ 5 9 6 0│
│ 3 1 3 2│
└+───────┘
  ⎕fmt h B
┌4──────────┐
3  a  b  c d│
│ 10 12 12 0│
│  3  1  3 2│
└+──────────┘
RosLuP
  • 153
  • 7