Contents

Develop
2016.10.15 12:24

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

Views 2000 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 •

?

  1. [ios] VIN Scanner (VIN barcode) 스캐너

    Date2017.09.16 CategoryDevelop Byhooni Views653
    Read More
  2. [android] 안드로이드 앱 문서 샘플 - NCComix

    Date2017.07.11 CategoryDevelop Byhooni Views2117
    Read More
  3. [coding] 공부해야 하는거 ㅋㅋ

    Date2017.06.27 CategoryDevelop Byhooni Views0
    Read More
  4. [coding] Find all anagrams in a string

    Date2017.06.27 CategoryDevelop Byhooni Views1150
    Read More
  5. [android] SQLiteOpenHelper를 이용한 DBManager

    Date2017.06.14 CategoryDevelop Byhooni Views2082
    Read More
  6. [android] 간단한 SQLIite 예제

    Date2017.06.14 CategoryDevelop Byhooni Views1339
    Read More
  7. 캘리포니아 운전면허 족보

    Date2017.06.12 CategoryDevelop Byhooni Views746
    Read More
  8. 사이버보안실무 발표자료 (2017.06.08)

    Date2017.06.05 CategoryDevelop Byhooni Views1224
    Read More
  9. 리팩토링 계획안

    Date2017.05.15 CategoryDevelop Byhooni Views759
    Read More
  10. [ios] 코코아 프로그래밍의 네이밍 룰(명명 규칙)

    Date2017.05.11 CategoryDevelop Byhooni Views1191
    Read More
  11. [ios] Facebook SDK 로그인 설명

    Date2017.04.19 CategoryDevelop Byhooni Views1171
    Read More
  12. [ios] NSString, RegularExpression Find/Replace

    Date2017.04.14 CategoryDevelop Byhooni Views848
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 71 Next
/ 71