Another way to replace the second column with 0:

mat =. 0 (<a:;1)} mat

monad m} is seldom a good solution.  I suggest

mat =. 10 (I.@:(1&=)@,@])} mat  NB. replace 1 values with 10

mat =. _ (I.@:(0 = 5 | ,@]))} mat  NB. replace multiples of 5 with _

for these unusual cases.


Operations on rows/columns do not have a special notation.  You must extract the row/col, operate on it, and replace it.  Use v0`v1`v2} to combine these actions:

mat =. 1 (+ {."1)`((<a:;0)"_)`]} mat  NB. add 1 to col 0, replace in col 0

mat =. 1 (+ 3&{)`(3:)`]}  NB. add 1 to row 3

For more complex stuff you need the details of (x m} y) in NuVoc. For scatter read where i is a table, each row of which is an index list of a desired cell, use

i (<"1@[ { ]) mat

For scatter modify, use

newvalues i} mat  NB. where i has rank > 1, giving the index list of each cell to be modified.

Henry Rich


On 2/6/2022 2:24 PM, Andrew P wrote:
I am trying to learn how to manipulate tables of numbers in J coming from a
NumPy/Python background. Assuming *mat* is a 10x10 matrix, that's what I
have figured out so far:

- Replace values equal to 1 with 10
(mat e. 1) } mat ,: 10
- Replace all non-multiples of 5 with _
(-. 0 = mat |~ 5) } mat ,: _
- Multiply each column by its index
mat (*" 1) i. 10
- Multiply each row by its index
mat (*" 0) i. 10
- Replace the second column with 0
0 (1) }"1 mat
- Replace the second column with 0 (another way)
0 (< (i.10); 1 ) } mat
- Take the diagonal of the matrix
(<0 1)&|: mat

Now, there are some tasks that I can do in NumPy but haven't found how to
do in J. For instance:
- Add 1 to each value in the first column
x[:,1] += 1
- Add 1 to each value in the third row
x[3,:] += 1

In each of those cases, I am selecting the cells by their indexes rather
than their values, and am using the existing values to generate the new
ones. Is that possible in J?

I would also like to figure out how to modify nontrivial sequences of
indexes, for instance, diagonals or cells where both the row index and the
column index are even numbers. Where should I start if I want to do that
kind of thing?

Any suggestion would be highly appreciated.


Andrew
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm


--
This email has been checked for viruses by AVG.
https://www.avg.com

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to