cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-195207%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EUtilizzo%20di%20Loc%20con%20una%20matrice%202D%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-195207%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-1624414572%22%20id%3D%22toc-hId-1624414665%22%20id%3D%22toc-hId-1624414665%22%3EProblema%3C%2FH2%3E%0A%3CP%3EHai%20una%20matrice%202D.%20Hai%20utilizzato%20la%20funzione%20Loc%20per%20individuare%20elementi%20interessanti%2C%20ma%20Loc%20ha%20restituito%20indici%20per%20una%20matrice%201D.%20Come%20%C3%A8%20possibile%20riconvertire%20gli%20indici%201D%20in%20indici%202D%3F%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--927742389%22%20id%3D%22toc-hId--927742296%22%20id%3D%22toc-hId--927742296%22%3ESoluzione%3C%2FH2%3E%0A%3CP%3EDovrai%20utilizzare%20le%20funzioni%20Floor%20e%20Mod%20e%20dovrai%20conoscere%20il%20numero%20di%20colonne%20nella%20tua%20matrice%202D.%20Ecco%20le%20tre%20righe%2C%20sepolte%20nell'esempio%20commentato%20di%20seguito.%20pixmat%20%C3%A8%20una%20matrice%202D%2C%20Loc%20trova%20i%20pixel%20diversi%20da%20zero%2C%20che%20sono%20neri%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Eblacklocs%20%3D%20(Loc(%20pixmat%20)%20-%201)%3B%20%2F%2F%20blacklocs%20%C3%A8%20una%20matrice%20di%20indici%201D%20(in%20base%200)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Erowlocs%20%3D%20Floor(%20(blacklocs)%20%2F%20nc%20)%20%2B%201%3B%20%2F%2F%20nc%20%C3%A8%20il%20numero%20di%20colonne%20nel%20pixmat%202D%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%20collocs%20%3D%20Mod(%20(blacklocs)%2C%20nc%20)%20%2B%201%3B%20%2F%2F%20rowlocs%20e%20collocs%20sono%20indici%202D%20(in%20base%201)%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F%2F%20we%20need%20a%20bitmap%20to%20work%20with%3B%20use%20a%20text%20box%20with%20a%20moderate%20size%20font%0Atextbox%20%3D%20Text%20Box(%20%22Gargoyle%22%2C%20%26lt%3B%3CSETFONTSIZE%3E%20.5%20are%20antialiasing%20fading%20to%20white%20background%20of%20the%20%22paper%22.%20%0A%2F%2F%20the%20next%20statement%20makes%20the%200%20to%20.5%20values%20be%201%20and%20the%20.5%20to%201%20values%20be%200%0Apixmat%20%3D%20pixmat%20%26lt%3B%20.75%3B%20%2F%2F%20cut%20off%20edge%20at%20some%20gray%20value.%20flip%20the%200..1%20significance.%0A%2F%2F%20the%20pixels%20are%20in%20a%202D%20matrix%20of%20rows%20and%20cols.%20get%20the%20dimensions.%0Anr%20%3D%20N%20Rows(%20pixmat%20)%3B%0Anc%20%3D%20N%20Cols(%20pixmat%20)%3B%0A%2F%2F%20now%20we%20can%20find%20the%20location%20of%20the%20black%20pixels%20(1s)%20that%20form%20the%20text.%0A%2F%2F%20the%20LOC%20function%20returns%20a%201D%20matrix%20of%20the%20offsets%20of%20non-zero%0A%2F%2F%20elements%20in%20the%202D%20matrix%20pixmat.%20if%20pixmat%20was%203%20rows%20of%205%20elements%20each%2C%0A%2F%2F%20the%20LOC%20could%20return%20a%201D%20matrix%20of%200%20to%2015%20elements%20with%20values%20from%201%20to%2015.%0A%2F%2F%20JMP%20uses%201-based%20indexing.%20subtract%20one%20to%20get%200-based%20for%20future%20steps.%0Ablacklocs%20%3D%20(Loc(%20pixmat%20)%20-%201)%3B%0A%2F%2F%20Here's%20the%20JSL%20that%20convers%20the%201D%20answers%20back%20to%202D%20answers.%0A%2F%2F%20use%20the%200-based%20blacklocs%20(previous%20statement)%2C%20then%20add%201%20to%20%0A%2F%2F%20turn%20the%200-based%20results%20back%20into%20the%201-based%20indexes%20JMP%20expects.%0Arowlocs%20%3D%20Floor(%20(blacklocs)%20%2F%20nc%20)%20%2B%201%3B%0Acollocs%20%3D%20Mod(%20(blacklocs)%2C%20nc%20)%20%2B%201%3B%0A%2F%2F%20Show%20the%20results%0Ascale%20%3D%2016%3B%20%2F%2F%20try%20bigger%20or%20smaller%20scale%20for%20your%20screen%20size%0ANew%20Window(%20%22Example%22%2C%0A%20%20%20%20Graph%20Box(%0A%20%20%20%20%20%20%20%20%2F%2F%20make%20the%20graph%20the%20same%20aspect%20ratio%20as%20the%20bitmap%0A%20%20%20%20%20%20%20%20framesize(%20nc%20*%20scale%2C%20nr%20*%20scale%20)%2C%20%0A%20%20%20%20%20%20%20%20%2F%2F%20make%20the%20point%20marker%20size%20big%3B%20.65%20is%20approximately%20right%0A%20%20%20%20%20%20%20%20%26lt%3B%3CMARKER%20size%3D%22%22%3E%3C%2FMARKER%3E%3C%2FSETFONTSIZE%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Pixels%20from%20a%20bitmap%2C%20displayed%20as%20markers%20in%20a%20graph%22%20style%3D%22width%3A%20999px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Pixels%20from%20a%20bitmap%2C%20displayed%20as%20markers%20in%20a%20graph%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Pixels%20from%20a%20bitmap%2C%20displayed%20as%20markers%20in%20a%20graph%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F17027i283F362ED3BE76FC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Capture.PNG%22%20alt%3D%22Pixels%20from%20a%20bitmap%2C%20displayed%20as%20markers%20in%20a%20graph%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPixels%20from%20a%20bitmap%2C%20displayed%20as%20markers%20in%20a%20graph%3C%2Fspan%3E%3C%2Fspan%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPixels%20from%20a%20bitmap%2C%20displayed%20as%20markers%20in%20a%20graph%3C%2FSPAN%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPixel%20di%20una%20bitmap%2C%20visualizzati%20come%20indicatori%20in%20un%20grafico%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-815067946%22%20id%3D%22toc-hId-815068039%22%20id%3D%22toc-hId-815068039%22%3EDiscussione%3C%2FH2%3E%0A%3CP%3EIl%20-1%20e%20il%20%2B1%20sono%20importanti%2C%20non%20lasciarli%20fuori.%20Leggi%20i%20commenti%20in%20base%201%20e%200%20nel%20JSL.%3C%2FP%3E%0A%3CP%3EIl%20testo%20nella%20bitmap%20%C3%A8%20anti-alias%2C%20il%20che%20significa%20che%20i%20pixel%20del%20bordo%20sono%20sfumature%20di%20grigio%20proporzionali%20a%20come%20il%20bordo%20perfetto%20del%20carattere%20sarebbe%20caduto%20tra%20i%20pixel.%20Ho%20scelto%20una%20soglia%20di%20grigio%20di%20.75%20perch%C3%A9%20il%20risultato%20mi%20piaceva%20di%20pi%C3%B9%20di%20.5%3B%20la%20tua%20applicazione%20probabilmente%20non%20se%20ne%20occuper%C3%A0%20affatto.%3C%2FP%3E%0A%3CP%3ELa%20funzione%20Shape()%20di%20JMP%20converte%20la%20forma%20di%20una%20matrice.%20Questo%20non%20aiuter%C3%A0%20qui.%3C%2FP%3E%0A%3CP%3EMolte%20delle%20istruzioni%20nell'esempio%20operano%20su%20una%20matrice%20senza%20utilizzare%20un%20ciclo%20for%20JSL%20esplicito.%20Buono%20per%20la%20velocit%C3%A0.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--1737089015%22%20id%3D%22toc-hId--1737088922%22%20id%3D%22toc-hId--1737088922%22%3EGuarda%20anche%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2F14-2%2Fmatrix-functions.shtml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2F14-2%2Fmatrix-functions.shtml%3C%2FA%3E%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-271547%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERi%3A%20Utilizzo%20di%20Loc%20con%20una%20matrice%202D%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-271547%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EGuarda%20anche%3A%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FWhat-is-the-JSL-Matrix%2Fm-p%2F33349%23U33349%22%20class%3D%22lia-mention-container-editor-message%20lia-img-icon-blog-thread%20lia-fa-icon%20lia-fa-blog%20lia-fa-thread%20lia-fa%22%20target%3D%22_blank%22%3E%20Cos'%C3%A8%20la%20matrice%20JSL%3F%3C%2FA%3E%20per%20un'introduzione%20alle%20matrici%20in%20JMP.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Using Loc with a 2D Matrix

Problem

You have a 2D matrix. You used the Loc function to locate interesting elements, but Loc returned indexes for a 1D matrix. How can you convert the 1D indexes back to 2D indexes?

Solution

You'll need to use the Floor and Mod functions and you'll need to know the number of columns in your 2D matrix. Here's the three lines, buried in the commented example below. pixmat is a 2D matrix, Loc is finding the non-zero pixels, which are black:

 

blacklocs = (Loc( pixmat ) - 1); // blacklocs is a matrix of 1D indexes (0-based)

rowlocs = Floor( (blacklocs) / nc ) + 1; // nc is the number of columns in the 2D pixmat
collocs = Mod( (blacklocs), nc ) + 1; // rowlocs and collocs are 2D indexes (1-based)

 

// we need a bitmap to work with; use a text box with a moderate size font
textbox = Text Box( "Gargoyle", <<setfontsize( 20 ) );
// get a picture from the text box
picture = textbox << getpicture;
// get the red pixels in a matrix. green or blue 
// would work as well, it is shades of gray.
pixmat = picture << getpixels("r"); // {[ red matrix]}
// the list would have more elements if you asked for "rgb".
// go ahead and pull out the only element.
pixmat = pixmat[1]; // [red matrix]
// the matrix of pixels is 0 for black, 1 for white, and .5 for a middle gray
// the values > .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( scale * .65 ), 
        // the x axis is the columns in the matrix
        xaxis( {Min( -1 ), Max( nc + 1 )} ), 
        // the y axis needs to flip the rows in the matrix
        yaxis( {Min( nr + 1 ), Max( -1 )} ), 
        // Marker takes a pair of matrices, one for the x, and one for they,
        // are draws a marker at each x-y position. The columns are x
        // and the rows are y. the yaxis, above, put row 1 at the top.
        Marker( collocs, rowlocs ) // <<<< here are the 2D coordinates <<<<
    )
);
Pixels from a bitmap, displayed as markers in a graphPixels from a bitmap, displayed as markers in a graph

Discussion

The -1 and +1 are important, don't leave them out. Read the 1-based and 0-based comments in the JSL.

The text in the bitmap is anti-aliased, which means the edge pixels are shades of gray proportional to how the perfect edge of the character would have fallen between pixels. I chose a gray threshold of .75 because I liked the result better than .5; your application probably won't be dealing with that at all.

JMP's Shape() function converts the shape of a matrix. That won't help here.

Many of the statements in the example are operating on a matrix without using an explicit JSL for-loop. Good for speed.

 


See Also

https://www.jmp.com/support/help/14-2/matrix-functions.shtml

Comments

see also: What is the JSL Matrix? for an introduction to matrices in JMP.

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.