Animated gif of random green letters with random red J, M, and P characters带有随机红色 J、M 和 P 字符的随机绿色字母的动画 gif
JMP 在 JSL 脚本语言中拥有一组功能强大的矩阵基元。 以下介绍了在 JSL 中创建和使用一维和二维矩阵。
3 行 x 2 列矩阵的 JSL 矩阵常数如下所示;请注意行上元素之间的空格以及行之间的逗号。
constant = [
1 2,
3 4,
5 6
];
当您运行该代码时,它将在日志窗口中显示如下(您也可以将其写在一行上):
[1 2, 3 4, 5 6]
方括号定义解析 JSL 时创建的常量。 您不能在方括号之间使用其他 JSL 变量,因为解析器在变量存在之前运行(稍后会详细介绍)。 另外两个使用一维矩阵的示例:
RowVector = [1 2];
ColVector = [3,4];
JSL 有多个创建矩阵的函数。 我通常使用 J( nRows, nCols, init ),其中 0 代表第三个在里面价值。
squareOnes = J(5);
[1 1 1 1 1,
1 1 1 1 1,
1 1 1 1 1,
1 1 1 1 1,
1 1 1 1 1]
diagOnes = identity(5);
[1 0 0 0 0,
0 1 0 0 0,
0 0 1 0 0,
0 0 0 1 0,
0 0 0 0 1]
colsAndRows = J(3,2,random integer(1,6));
[2 6, 2 5, 5 2]
您还可以通过水平或垂直连接值来构建矩阵。 相同的串联运算符将连接具有兼容大小的矩阵(此处未显示)。
a=1;b=2;c=3;d=4;e=5; // now we can use variables as follows
row1 = a::e; // row
[1 2 3 4 5]
col1 = (a::e)`; // column - transpose operator
[1, 2, 3, 4, 5]
row2 = a||22||c||44||e; // row - concatenate horizontal
[1 22 3 44 5]
col2 = a|/222|/c|/444|/e; // column - concatenate vertical
[1, 222, 3, 444, 5]
那个小小的 ` 字符位于美国键盘的左上角,是矩阵转置运算符。 :: 运算符创建一个整数矩阵。 您可以乘以一个标量并添加一个标量以获得所需的结果矩阵。
(0::9)*.1+100
[100 100.1 100.2 100.3 100.4 100.5 100.6 100.7 100.8 100.9]
矩阵以通常的方式添加下标。 JSL 中矩阵下标从 1 开始,而不是像某些语言中那样从 0 开始。
M = [
11 12 13 14 15,
21 22 23 24 25,
31 32 33 34 45
];
write(M);
[ 11 12 13 14 15,
21 22 23 24 25,
31 32 33 34 45]
write(M[2,3])
23
并且可以通过强大的方式对它们进行切片和切块:
write(M[2::3, 1::2]); // return a 2x2 sub-matrix
[21 22, 31 32]
write(M[1,0]); // 0 means the entire dimension
[11 12 13 14 15]
write(M[0,2::3]); // various combinations
[12 13, 22 23, 32 33]
JSL 中有一个称为 shape 的矩阵函数,有时可用于将行向量或列向量重塑为 2D 矩阵。 矩阵有两个乘法运算符,一个用于矩阵代数乘法 ( m1 * m2 ),另一个用于逐个元素乘法:
[1 2, 3 4]
[2 3, 4 5]
[2 6, 12 20]
请记住:*如果将两个图像相乘得到真正意想不到的结果!
您也可以为矩阵元素或切片赋值:
mat = J(2,3,.); // 2 rows, three columns, all missing
mat[1,1] = 7; // the top left corner
mat[0,2::3] = [5 9, 13 15]; // all rows, columns 2 and 3
write(mat); // notice the missing value that never got set
[7 5 9, . 13 15]
最后,这是 gif 动画的代码几乎与主题无关。 它以令人惊讶的方式使用矩阵来操作图像和字符,并且可能不是您想要开始的示例。
matgif = "$desktop/mat.gif";
gb = Graph Box(framesize(500,300));
gb << setBackgroundColor( "black" );
gb << setGraphicsScript(
Text Color( "green" );
For( x = -10, x <= 110, x += Random Integer( 2, 4 ),
For( y = -10, y <= 110, y += Random Integer( 3, 7 ),
c = random integer(31,1000);
textcolor("green");
if( random integer(1,10000)<60,
textcolor("red");
c = blobtomatrix(chartoblob(substr("JMP", random integer(1,3),1)),"uint",1,"big")
);
Text( {x, y}, blobtochar(matrixtoblob(c::c,"uint",2,"big"),encoding="utf-16be") )
);
);
);
New Window( "x", gb );
duration=80;//ms
animation = gb[framebox(1)]<<GETPICTURE></GETPICTURE>
https://community.jmp.com/t5/Discussions/Near-neighbors-mean-calculation/mp/345727#M59633有一个使用另一个矩阵 indexer2x2 索引矩阵 M3 的示例,以获得 M3 的子矩阵。 此示例旨在获取连续的矩形区域并将其作为 2D 矩阵返回,但索引器可以以任何顺序获取任何元素集,如果您想要项目的 1D 矩阵,则 shape() 函数是可选的:
M3 = [. . . . . . . . .,
. . . . . . . . .,
. . . . . . . . .,
. . . 1 2 3 . . .,
. . . 4 5 6 . . .,
. . . 7 8 9 . . .,
. . . . . . . . .,
. . . . . . . . .,
. . . . . . . . .];
indexer2x2 = [1 2 10 11]; // top left 2x2 sub-matrix
For( x = 2, x <= 5, x += 1,
For( y = 2, y <= 5, y += 1,
Write( "\!n ", x, " ", y, " ", Shape( M3[indexer2x2 + x + y * 9], 2 ) );
)
);
2 2 [。 ., . 1]
2 3 [。 1、. 4]
2 4 [。 4、. 7]
2 5 [。 7、. .]
3 2 [。 ., 1 2]
3 3 [1 2, 4 5]
3 4 [4 5, 7 8]
3 5 [7 8,. .]
4 2 [。 ., 2 3]
4 3 [2 3, 5 6]
4 4 [5 6, 8 9]
4 5 [8 9,. .]
5 2 [。 ., 3 .]
5 3 [3 ., 6 .]
5 4 [6 ., 9 .]
5 5 [9 ., . .]