2

I have a 5x5 matrix M and a function f that operates on vectors. r ← ⊃?⍴M is the index of a random row of M. I need to return a modified version of M without direct assignment (it's important for the larger program) where f is only applied to r⌷M. I'm doing this by mapping across M's rows, returning f⍵ if the row's index matches r, and just if it doesn't. The function I came up with is:

({f(⍣(r = M⍳⍵))⍵}⍤1) M

It works, but it's not ideal. I don't like the r = M⍳⍵ part, because I'm searching for the index of in every cycle. I think it'd make more sense to operate across ⍳5 instead, referencing each row in terms of each in this vector. I can't seem to get this to work though.

Any help making my function less ugly is appreciated.

Ian Martin
  • 95
  • 6
  • 1
    Btw, you should use `r←?≢M` instead of `r←⊃?⍴M`. Monadic `≢` is *tally*, which is the number of major cells, so the number of rows of a matrix. – Adám Apr 28 '18 at 21:34

1 Answers1

4

f@r⊢M­

Example code:

M←5 5⍴⍳25
f←-
r←⊃?⍴M
⎕←f@r⊢M

Example result:

 1  2  3  4   5
¯6 ¯7 ¯8 ¯9 ¯10
11 12 13 14  15
16 17 18 19  20
21 22 23 24  25
EKons
  • 887
  • 2
  • 20
  • 27
ngn
  • 7,763
  • 6
  • 26
  • 35
  • Very cool, thank you. I'm not sure what the right tack is doing here though. `f@r⌷M` yields a similar result. Is there a reason to prefer the former? – Ian Martin Apr 28 '18 at 11:35
  • 1
    `⊢` prevents `r` and `M` from "stranding" (forming a vector). Another way to do that is with parentheses: `(f@r)M`. Note that `r` is not the left argument to `⊢` (or `⌷` in your example) but the right operand to `@`, which is a dyadic operator. – ngn Apr 28 '18 at 13:24
  • 1
    @IanMartin The difference between monadic `⊢` and monadic `⌷` is that `⌷` will materialise the default property of a class and the constituent members of a collection object, while `⊢` is a true no-op. – Adám Apr 28 '18 at 21:36