Develop
2016.10.15 12:24

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

Views 1994 Votes 0 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
997 PPT [ppt] 웜 과제 진행사항(프레임워크 검증환경 구축) 보고 file hooni 2013.04.23 15895
996 PPT [ppt] 시스템보안 연구실 ppt 템플릿.. (CIST) file hooni 2013.04.23 20871
995 PPT [ppt] 보안경제학 발표자료 secret hooni 2016.11.11 5
994 PPT [ppt] 뚜레쥬르 온라인 마케팅 제안서 file hooni 2013.04.23 23499
993 PPT [ppt] 네오웹 소프트.. 발표 자료 모음 file hooni 2003.04.23 12550
992 PPT [ppt] OCB기술개발팀 OJT 자료 file hooni 2015.11.13 1162
991 PPT [ppt] Magic URLs & Hidden Form Fields 에 대해.. ㅋㅋ file hooni 2013.04.23 12094
990 PPT [ppt] Macro for board game 발표자료 (@Team Study 2013.01.18) 1 file hooni 2015.07.22 1278
989 Develop [ppt] iOS 플라랩#04(2015.06.19) 발표 자료 file hooni 2015.06.04 638
988 PPT [ppt] iOS 플라랩#03(2015.04.27) 발표 자료 file hooni 2015.04.24 1066
987 PPT [ppt] iOS 플라랩#02(2015.03.19) 발표 자료 file hooni 2015.04.24 904
986 PPT [ppt] iOS 플라랩#01(2015.02.26) 발표 자료 file hooni 2015.02.25 738
985 PPT [ppt] Information Security 발표 자료 (@Team Study 2012.11.15) file hooni 2015.07.22 887
984 PPT [ppt] Equation Solving 발표 자료 (@PWE랩 톡데이 2011.07.26) file hooni 2013.04.23 24673
983 PPT [ppt] Equation Solving 발표 자료 (@AjaxUI랩 밋업데이 2012.02.28) file hooni 2015.07.22 916
982 Develop [php][laravel] 초간단 MacOS에서 Laravel 개발 환경 구축 hooni 2017.12.15 1886
Board Pagination Prev 1 ... 10 11 12 13 14 ... 74 Next
/ 74