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>
<P> </P>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 ., . .]