問題
2D行列があります。 Loc 関数を使用して興味深い要素を見つけましたが、Loc は 1D 行列のインデックスを返しました。 1D インデックスを 2D インデックスに変換するにはどうすればよいですか?
解決
Floor 関数と Mod 関数を使用する必要があり、2D 行列の列数を知る必要があります。 以下の 3 行は、以下のコメント付きの例に埋め込まれています。 pixmat は 2D 行列で、Loc は黒のゼロ以外のピクセルを見つけます。
blacklocs = (Loc( pixmat ) - 1); // blacklocs は 1D インデックスの行列です (0 ベース)
rowlocs = Floor( (blacklocs) / nc ) + 1; // nc は 2D pixmat の列数です
Collocs = Mod( (blacklocs), nc ) + 1; // rowloc と coloc は 2D インデックス (1 から始まる)
// we need a bitmap to work with; use a text box with a moderate size font
textbox = Text Box( "Gargoyle", <<SETFONTSIZE> .5 are antialiasing fading to white background of the "paper".
// the next statement makes the 0 to .5 values be 1 and the .5 to 1 values be 0
pixmat = pixmat < .75; // cut off edge at some gray value. flip the 0..1 significance.
// the pixels are in a 2D matrix of rows and cols. get the dimensions.
nr = N Rows( pixmat );
nc = N Cols( pixmat );
// now we can find the location of the black pixels (1s) that form the text.
// the LOC function returns a 1D matrix of the offsets of non-zero
// elements in the 2D matrix pixmat. if pixmat was 3 rows of 5 elements each,
// the LOC could return a 1D matrix of 0 to 15 elements with values from 1 to 15.
// JMP uses 1-based indexing. subtract one to get 0-based for future steps.
blacklocs = (Loc( pixmat ) - 1);
// Here's the JSL that convers the 1D answers back to 2D answers.
// use the 0-based blacklocs (previous statement), then add 1 to
// turn the 0-based results back into the 1-based indexes JMP expects.
rowlocs = Floor( (blacklocs) / nc ) + 1;
collocs = Mod( (blacklocs), nc ) + 1;
// Show the results
scale = 16; // try bigger or smaller scale for your screen size
New Window( "Example",
Graph Box(
// make the graph the same aspect ratio as the bitmap
framesize( nc * scale, nr * scale ),
// make the point marker size big; .65 is approximately right
<<MARKER size=""></MARKER></SETFONTSIZE>
Pixels from a bitmap, displayed as markers in a graphPixels from a bitmap, displayed as markers in a graphグラフ内のマーカーとして表示されるビットマップのピクセル
議論
-1 と +1 は重要なので省略しないでください。 JSL の 1 から始まるコメントと 0 から始まるコメントを読みます。
ビットマップ内のテキストはアンチエイリアス処理されています。つまり、エッジ ピクセルは、文字の完全なエッジがピクセル間にどのように収まるかに比例したグレーの色合いになります。 グレーのしきい値として 0.75 を選択したのは、結果が 0.5 よりも優れていたためです。あなたのアプリケーションはおそらくそれをまったく処理しないでしょう。
JMP の Shape() 関数は、行列の形状を変換します。 それはここでは役に立ちません。
この例のステートメントの多くは、明示的な JSL for ループを使用せずに行列上で動作します。 スピードに優れています。
関連項目
https://www.jmp.com/support/help/14-2/matrix-functions.shtml
以下も参照してください。 JSLマトリックスとは何ですか? JMP の行列の概要については、「JMP の行列の概要」を参照してください。