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+

4
  • 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? Commented Feb 23, 2020 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
    Commented Feb 23, 2020 at 12:50
  • 1
    @AbhaySehgal I've clarified that "17+" refers to Dyalog APL 17.0 or newer.
    – Adám
    Commented Feb 23, 2020 at 12:59
  • 1
    @cickness You might be interested in frequenting the Stack Exchange APL chat room, where you'll find both beginner and expert APLers.
    – Adám
    Commented Feb 23, 2020 at 13:02

2 Answers 2

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!

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│
└+──────────┘

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.