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 second row.

How do I get the character corresponding to the highest number in the second row, and how do I increase the corresponding number in the second row? (For example, here, column b has the highest number, 9, so increase that number by 10%.)

I use Dyalog version 17.1.

Adám
  • 6,573
  • 20
  • 37
cickness
  • 737
  • 2
  • 6
  • 13

1 Answers1

1

With:

      ⎕←data←3 4⍴'a' 'b' 'c' 'd' 5 9 6 0 3 1 3 2
a b c d
5 9 6 0
3 1 3 2

You can extract the second row with:

      2⌷data
5 9 6 0

Now grade it descending, that is, find the indices that would sort it from highest to lowest:

      ⍒2⌷data
2 3 1 4

The first number is the column we're looking for:

      ⊃⍒2⌷data
2

Now we can use this to extract the character from the first row:

      data[⊂1,⊃⍒2⌷data]
b

But we only need the column index, not the actual character. The full index of the number we want to increase is:

     2,⊃⍒2⌷data
2 2

Extracting the data to see that we got the right index:

      data[⊂2,⊃⍒2⌷data]
9

Now we can either create a new array with the target value increased by 10%:

      1.1×@(⊂2,⊃⍒2⌷data)⊢data
a   b c d
5 9.9 6 0
3 1   3 2

Or change it in-place:

      data[⊂2,⊃⍒2⌷data]×←1.1
      data
a   b c d
5 9.9 6 0
3 1   3 2

Try it online!

Adám
  • 6,573
  • 20
  • 37
  • can you explain how makes this situation? How to bind with 3rd row? for example, if the number in the third row is 3, then we multiply the number in the second row by 2, and if 1 then we multiply by 1.5 –  cickness Feb 23 '20 at 11:29
  • @cickness I suggest you ask a new question for that, and make sure to explain what exactly is the relationship that makes a 3 multiply by 2 but a 1 multiply by 1.5. Is is a mathematical formula or a look-up table? – Adám Feb 23 '20 at 11:41