문제
2D 매트릭스가 있습니다. Loc 함수를 사용하여 흥미로운 요소를 찾았지만 Loc은 1D 행렬에 대한 인덱스를 반환했습니다. 1D 인덱스를 2D 인덱스로 다시 변환하는 방법은 무엇입니까?
해결책
Floor 및 Mod 기능을 사용해야 하며 2D 매트릭스의 열 수를 알아야 합니다. 다음은 아래 주석이 달린 예에 묻힌 세 줄입니다. pixmat는 2D 매트릭스이고 Loc은 검은색인 0이 아닌 픽셀을 찾습니다.
blacklocs = (Loc( pixmat ) - 1); // blacklocs는 1D 인덱스의 행렬입니다(0 기반).
rowlocs = Floor( (블랙록) / nc ) + 1; // nc는 2D pixmat의 열 수입니다.
collocs = Mod( (블랙록스), nc ) + 1; // rowlocs 및 collocs는 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 기반 주석을 읽으십시오.
비트맵의 텍스트는 앤티 앨리어싱되어 가장자리 픽셀이 문자의 완벽한 가장자리가 픽셀 사이에 떨어지는 정도에 비례하는 회색 음영임을 의미합니다. 결과가 .5보다 더 좋아서 .75의 회색 임계값을 선택했습니다. 귀하의 응용 프로그램은 아마도 전혀 처리하지 않을 것입니다.
JMP의 Shape() 함수는 행렬의 모양을 변환합니다. 여기서는 도움이 되지 않습니다.
예제의 많은 명령문은 명시적 JSL for-loop를 사용하지 않고 행렬에서 작동합니다. 속도에 좋습니다.
또한보십시오
https://www.jmp.com/support/help/14-2/matrix-functions.shtml
또한보십시오: JSL 매트릭스란 무엇입니까? JMP의 행렬 소개.