Problem
Sie haben eine 2D-Matrix. Sie haben die Loc-Funktion verwendet, um interessante Elemente zu finden, aber Loc hat Indizes für eine 1D-Matrix zurückgegeben. Wie können Sie die 1D-Indizes wieder in 2D-Indizes konvertieren?
Lösung
Sie müssen die Funktionen Floor und Mod verwenden und die Anzahl der Spalten in Ihrer 2D-Matrix kennen. Hier sind die drei Zeilen, die im kommentierten Beispiel unten vergraben sind. pixmat ist eine 2D-Matrix, Loc findet die Nicht-Null-Pixel, die schwarz sind:
blacklocs = (Loc( pixmat ) - 1); // blacklocs ist eine Matrix aus 1D-Indizes (0-basiert)
rowlocs = Floor( (blacklocs) / nc ) + 1; // nc ist die Anzahl der Spalten im 2D-Pixmat
collocs = Mod( (blacklocs), nc ) + 1; // Rowlocs und Collocs sind 2D-Indizes (1-basiert)
// 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 graphPixel aus einer Bitmap, die als Markierungen in einem Diagramm angezeigt werden
Diskussion
-1 und +1 sind wichtig, lassen Sie sie nicht weg. Lesen Sie die 1-basierten und 0-basierten Kommentare in der JSL.
Der Text in der Bitmap ist geglättet, was bedeutet, dass die Kantenpixel Graustufen aufweisen, die proportional dazu sind, wie die perfekte Kante des Zeichens zwischen den Pixeln gelegen hätte. Ich habe einen Grauschwellenwert von 0,75 gewählt, weil mir das Ergebnis besser gefiel als 0,5; Ihre Bewerbung wird sich damit wahrscheinlich überhaupt nicht befassen.
Die Shape()-Funktion von JMP konvertiert die Form einer Matrix. Das wird hier nicht helfen.
Viele der Anweisungen im Beispiel arbeiten mit einer Matrix, ohne eine explizite JSL-For-Schleife zu verwenden. Gut für die Geschwindigkeit.
Siehe auch
https://www.jmp.com/support/help/14-2/matrix-functions.shtml
siehe auch: Was ist die JSL-Matrix? für eine Einführung in Matrizen in JMP.