Contents

Develop
2016.10.15 12:24

[matlab] ZigZag-Scanning (2-D Array)

Views 1994 Comment 0
Atachment
Attachment '12'
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

[MatLab] ZigZag-Scanning (2-D Array)


# ZigZag-Scanning 과제 내용

주어진 2차원 배열을 다음 그림과 같이 지그재그 형태로 탐색하여 1차원 배열의 결과물을 만드는 MatLab 코드를 만들어야 함.


zigzag.png

[이미지] https://en.wikipedia.org/wiki/JPEG

[참고] https://en.wikipedia.org/wiki/File:JPEG_ZigZag.svg



# MatLab 실행 영상



# 구현 방법

다음과 같은 2차원(3x3) 배열 Q가 있다고 할 때,


no_01.png



1. 원본 Q 배열의 좌/우를 뒤집는다. (fliplr() 함수 사용)

결과는 다음 afterFliplr1 과 같다.


no_02.png



2. 위의 1번의 결과 배열에서 대각 배열을 만든다. (spdiags() 함수 사용)

결과는 다음 afterSpdiags 와 같다.


no_03.png



3. 위의 2번의 결과 배열의 좌/우를 뒤집는다. (fliplr() 함수 사용)

결과는 다음 afterFliplr2 와 같다.


no_04.png



4. 위의 3번의 결과 배열의 홀수번째 컬럼의 위/아래를 뒤집는다. (flipud()함수 사용)

결과는 다음 afterFlipud 과 같다.


no_05.png



5. 위의 5번의 결과 배열에서 0을 제거하고 1차원 배열로 출력한다.

결과는 다음 orderNonZero 와 같다.


no_06.png


6. 위의 5번의 결과는 Q 배열을 탐색하는 인덱스의 순서이다.

MatLab에서 배열 인덱스는 1부터 시작하므로, 

원본 Q 배열을 orderNonZero 인덱스의 순서대로 탐색하여 Zig-Zag 1차원 배열을 만들 수 있다.

결과는 다음 ZigZag 와 같다.


no_07.png



# MatLab 소스코드 (Ver.1)

clear all; clc;

%% Test elements #1
Q = [1 2 3;
     4 5 6;
     7 8 9];
%% Test elements #2
Q = [01 02 06 07;
     03 05 08 13;
     04 09 12 14;
     10 11 15 16];
 
%% Init
tmp = reshape(1:numel(Q), size(Q));

%% Flip left/right -> Diagonal matrix -> Flip left/right again
afterFliplr1 = fliplr( tmp );
afterSpdiags = spdiags( afterFliplr1 );
afterFliplr2 = fliplr( afterSpdiags );

%% Flip up/down(odd columns)
afterFlipudOdd = afterFliplr2;
afterFlipudOdd(:,1:2:end) = flipud( afterFliplr2(:,1:2:end) );

%% Remove zero
orderNonZero = afterFlipudOdd;
orderNonZero( orderNonZero==0 ) = [];

%% Get elements (by zigzag-order)
ZigZag = Q(orderNonZero)

%keyboard



# MatLab 소스코드 (Ver.2)

clear all;
clc;

%% ZigZag %%

%% Test array1
Q = [1 2 3;
     4 5 6;
     7 8 9];
%% Test array2
Q = [01 02 06 07;
     03 05 08 13;
     04 09 12 14;
     10 11 15 16];

%% Init
tmp = reshape(1:numel(Q), size(Q));

%% Flip left/right -> Diagonal matrix -> Flip left/right again
order = fliplr( spdiags( fliplr(tmp) ) );

%% Flip up/down(odd columns)
order(:,1:2:end) = flipud( order(:,1:2:end) );

%% Remove zero
order(order==0) = [];

%% Get elements (by zigzag-order)
ZigZag = Q(order)

keyboard


# 관련 함수 설명

reshape.png


fliplr.png


spdiags.png


flipud.png



TAG •

?

List of Articles
No. Category Subject Author Date Views
793 Develop [ios] VIN Scanner (VIN barcode) 스캐너 file hooni 2017.09.16 650
792 Develop [android] 안드로이드 앱 문서 샘플 - NCComix file hooni 2017.07.11 2111
791 Develop [coding] 공부해야 하는거 ㅋㅋ secret hooni 2017.06.27 0
790 Develop [coding] Find all anagrams in a string hooni 2017.06.27 1146
789 Develop [android] SQLiteOpenHelper를 이용한 DBManager hooni 2017.06.14 2077
788 Develop [android] 간단한 SQLIite 예제 hooni 2017.06.14 1334
787 Develop 캘리포니아 운전면허 족보 file hooni 2017.06.12 742
786 Develop 사이버보안실무 발표자료 (2017.06.08) file hooni 2017.06.05 1218
785 Develop 리팩토링 계획안 file hooni 2017.05.15 755
784 Develop [ios] 코코아 프로그래밍의 네이밍 룰(명명 규칙) hooni 2017.05.11 1187
783 Develop [ios] Facebook SDK 로그인 설명 file hooni 2017.04.19 1164
782 Develop [ios] NSString, RegularExpression Find/Replace hooni 2017.04.14 844
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 71 Next
/ 71