-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigzagscan.m
70 lines (64 loc) · 2.43 KB
/
zigzagscan.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function vector = zigzagscan(matrix)
%zigzagsacn: function for zigzag scan
[h, w] = size(matrix);
if h == 8 && w == 8
vector8 = reshape(matrix, 1, 64);
index = [1, 9, 2, 3, 10, 17, 25, 18, ...
11, 4, 5, 12, 19, 26, 33, 41, ...
34, 27, 20, 13, 6, 7, 14, 21, ...
28, 35, 42, 49, 57, 50, 43, 36, ...
29, 22, 15, 8, 16, 23, 30, 37, ...
44, 51, 58, 59, 52, 45, 38, 31, ...
24, 32, 39, 46, 53, 60, 61, 54, ...
47, 40, 48, 55, 62, 63, 56, 64];
vector = vector8(index);
else
vector = zeros(1, h * w);
row = 1;
col = 1;
pos = 1;
direction = [-1, 1];
% first for row and second for col
while row <= h && col <= w
vector(pos) = matrix(row, col);
if isequal(direction, [-1, 1]) && row == 1 && col ~= w
direction = [0, 1];
col = col + 1;
elseif isequal(direction, [-1, 1]) && col == w
direction = [1, 0];
row = row + 1;
elseif isequal(direction, [-1, 1]) && col ~= w && row ~= 1
direction = [-1, 1];
row = row - 1;
col = col + 1;
elseif isequal(direction, [1, -1]) && col == 1 && row ~= h
direction = [1, 0];
row = row + 1;
elseif isequal(direction, [1, -1]) && row == h
direction = [0, 1];
col = col + 1;
elseif isequal(direction, [1, -1]) && row ~= h && col ~= 1
direction = [1, -1];
col = col - 1;
row = row + 1;
elseif isequal(direction, [1, 0]) && col == 1
direction = [-1, 1];
row = row - 1;
col = col + 1;
elseif isequal(direction, [1, 0]) && col == w
direction = [1, -1];
col = col - 1;
row = row + 1;
elseif isequal(direction, [0, 1]) && row == 1
direction = [1, -1];
col = col - 1;
row = row + 1;
elseif isequal(direction, [0, 1]) && row == h
direction = [-1, 1];
row = row - 1;
col = col + 1;
end
pos = pos + 1;
end
end
end