-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzigzag.m
89 lines (67 loc) · 2.05 KB
/
zigzag.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
% Zigzag scan of a matrix
% Argument is a two-dimensional matrix of any size,
% not strictly a square one.
% Function returns a 1-by-(m*n) array,
% where m and n are sizes of an input matrix,
% consisting of its items scanned by a zigzag method.
%
% Alexey S. Sokolov a.k.a. nICKEL, Moscow, Russia
% June 2007
function output = zigzag(in)
% initializing the variables
%----------------------------------
h = 1;
v = 1;
vmin = 1;
hmin = 1;
vmax = size(in, 1);
hmax = size(in, 2);
i = 1;
output = zeros(1, vmax * hmax);
%----------------------------------
while ((v <= vmax) & (h <= hmax))
if (mod(h + v, 2) == 0) % going up
if (v == vmin)
output(i) = in(v, h); % if we got to the first line
if (h == hmax)
v = v + 1;
else
h = h + 1;
end;
i = i + 1;
elseif ((h == hmax) & (v < vmax)) % if we got to the last column
output(i) = in(v, h);
v = v + 1;
i = i + 1;
elseif ((v > vmin) & (h < hmax)) % all other cases
output(i) = in(v, h);
v = v - 1;
h = h + 1;
i = i + 1;
end;
else % going down
if ((v == vmax) & (h <= hmax)) % if we got to the last line
output(i) = in(v, h);
h = h + 1;
i = i + 1;
elseif (h == hmin) % if we got to the first column
output(i) = in(v, h);
if (v == vmax)
h = h + 1;
else
v = v + 1;
end;
i = i + 1;
elseif ((v < vmax) & (h > hmin)) % all other cases
output(i) = in(v, h);
v = v + 1;
h = h - 1;
i = i + 1;
end;
end;
if ((v == vmax) & (h == hmax)) % bottom right element
output(i) = in(v, h);
break
end;
end;