简体   繁体   English

如何为 J 中的三列数字编写函数?

[英]How to write a function for three columns of numbers in J?

I need to write a function that I just need to input three cols of number and it will out puts a col of number.我需要编写一个函数,我只需要输入三列数字,它就会输出一列数字。 So they can calculate in each row and finally get just one row number.所以他们可以在每一行计算,最后只得到一个行号。 For example,X= 1 2 3, Y = 2 3 4, Z= 0 1 2, calculate (xz)/(yz), so the output is 0.5 0.5 0.5例如,X= 1 2 3, Y = 2 3 4, Z= 0 1 2,计算(xz)/(yz),所以输出为0.5 0.5 0.5

I think that you have pretty much stated how it could be solved if you convert your math notation to J (also use =: as assignment rather than = )我认为,如果您将数学符号转换为 J(也使用=:作为赋值而不是= ),您几乎已经说明了如何解决它

   X=: 1 2 3
   Y=: 2 3 4
   Z=: 0 1 2
   (X-Z)%(Y-Z) NB. doesn't need second parenthesis pair (X-Z)%Y-Z also works
0.5 0.5 0.5

Reading again you wanted columns of numbers instead of rows, so we'll just change the rows to columns using monadic ,.再次阅读您需要数字列而不是行,因此我们将使用 monadic ,.将行更改为列,. "Ravel Items" which takes each item and makes it a row. “Ravel Items”,它获取每个项目并将其排成一行。 In this case the each item is an atom and the rows are made into columns and the result is a column.在这种情况下,每一项都是一个原子,行被分成列,结果是一列。

   ex=: ,.X
   why=: ,. Y
   zed=: ,. Z
   ex
1
2
3
   why
2
3
4
   zed
0
1
2
   (ex-zed)%why-zed
0.5
0.5
0.5

If this is related to your other question , you can box or shape the arguments to pass arbitrary shapes:如果这与您的其他问题有关,您可以将参数装箱或整形以传递任意形状:

 f =: 3 : 0                                                                                                                                                 
'x y z' =. y
(x-z)%(y-z)                                                                                                                                                  
)

f 1 2 3
 2    
f (1 2 3);(2 3 4);(0 1 2)
 0.5 0.5 0.5

f (2 3 $ 5 4);(2 3$ 6 4 2);(2 3$ 7 5)
 2        1      0.4
_1 0.666667 0.333333


f i.3 2 4 3  NB. Input shape (3 a b c...), output shape a b c...
 2 2 2
 2 2 2
 2 2 2
 2 2 2

 2 2 2
 2 2 2
 2 2 2
 2 2 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM