Contents

Develop
2016.10.15 12:24

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

조회 수 1973 댓글 0
Atachment
첨부 '12'
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

[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
번호 분류 제목 글쓴이 날짜 조회 수
1077 System/OS [linux] ipchains 사용예(패킷 필터링) hooni 2003.04.23 14165
1076 System/OS [linux] ipfwadm를 이용한 패킷필터링(구버전) hooni 2003.04.23 13009
1075 System/OS [windows] 98/ME 속도 빠르게 튜닝(부팅,메모리,레지스터) hooni 2003.04.23 50433
1074 System/OS [windows] 도스 사용 팁 hooni 2003.04.23 17800
1073 System/OS [windows] 여러가지 활용 팁 hooni 2003.04.23 19963
1072 System/OS [windows] 배치(bat)파일 제작 방법 hooni 2003.04.23 16996
1071 System/OS [linux] 패킷 스니퍼링 hooni 2003.04.23 15897
1070 System/OS [linux] 패킷의 소스 주소 바꾸기 hooni 2003.04.23 18778
1069 System/OS [linux] iptables 명령어 매뉴얼(options) hooni 2003.04.23 11327
1068 System/OS [linux] 간단한 NAT 설정 script hooni 2003.04.23 12589
1067 System/OS [linux] 랜카드 2개 설정 & iptables 로 사설 ip.. hooni 2003.04.23 14724
1066 System/OS [linux] apache, php, jsp 환경설정하기.. hooni 2003.04.23 15224
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 98 Next
/ 98