changeset 2660:068affeed38b octave-forge

zagzig & zigzag functions from Fredrik Bulow, for use with matrix processing
author gnumuthu
date Tue, 10 Oct 2006 19:24:34 +0000
parents 6af5e46abef9
children efc472a6e3fe
files main/miscellaneous/inst/zagzig.m main/miscellaneous/inst/zigzag.m
diffstat 2 files changed, 181 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/miscellaneous/inst/zagzig.m	Tue Oct 10 19:24:34 2006 +0000
@@ -0,0 +1,92 @@
+## Copyright (C) 2006, October, Fredrik Bulow, <fredrik.bulow@gmail.com>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+##
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} zagzig (@var{mtrx})
+## Returns zagzig walk-off of the elements of @var{mtrx}.
+## Essentially it walks the matrix in a Z-fashion.
+##  
+## mat = 
+##   1   4   7
+##   2   5   8
+##   3   6   9
+## then zagzag(mat) gives the output,
+## [1 4 2 3 5 7 8 6 9], by walking as
+## shown in the figure from pt 1 in that order of output.
+## The argument @var{mtrx} should be a MxN matrix. One use of
+## zagzig the use with picking up DCT coefficients
+## like in the JPEG algorithm for compression.
+##
+## An example of zagzig use:
+## @example
+## @group
+## mat = reshape(1:9,3,3);
+## zagzag(mat)
+## ans =[1 4 2 3 5 7 8 6 9]
+##
+## @end group
+## @end example
+##
+## @end deftypefn
+## @seealso{zigzag}
+
+## Author:   Fredrik Bulow, <fredrik.bulow@gmail.com>
+
+function rval = zagzig(mtrx)
+
+  if nargin != 1 #Checking arguments.
+    error('usage: zagzig(matrix); see help zagzig');
+  endif
+
+  if issquare(mtrx) #Square matrix (quick case)
+    n=length(mtrx);
+    ##We create a matrix of the same size as mtrx where odd elements are
+    ##1, others 0.
+    odd=kron(ones(n,n),eye(2))((1:n),(1:n));
+
+    ##We transpose even elements only.
+    mtrx = (mtrx.*odd)' + (mtrx.*(1-odd));
+
+    ##Now we mirror the matrix. The desired vector is now the
+    ##concatenation of the diagonals.
+    mtrx=mtrx(:,1+size(mtrx,2)-(1:size(mtrx,2)));
+
+    ##Picking out the diagonals.
+    rval  = [];
+    for i = n-1:-1:1-n
+      rval=[rval diag(mtrx,i)'];
+    endfor
+
+  else #Not square (Slow cases)
+    n=size(mtrx);
+    mtrx=mtrx(:,1+size(mtrx,2)-(1:size(mtrx,2)));
+
+    ##Picking out the diagonals and reversing odd ones manually.
+    rval  = [];
+    for i = n(2)-1:-1:1-n(1)
+      new = diag(mtrx,i);
+      if floor(i/2)==i/2 ##Even?
+	rval=[rval new((1+length(new))-(1:length(new)))'];
+      else                ##Odd!
+	rval=[rval new'];
+      endif
+    endfor
+  endif
+endfunction
+%!
+%!assert(zagzig(reshape(1:9,3,3)),[1 4 2 3 5 7 8 6 9])
+%!
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/miscellaneous/inst/zigzag.m	Tue Oct 10 19:24:34 2006 +0000
@@ -0,0 +1,89 @@
+## Copyright (C) 2006, October, Fredrik Bulow, <fredrik.bulow@gmail.com>
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+##
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} zigzag (@var{mtrx})
+## Returns zigzag walk-off of the elements of @var{mtrx}.
+## Essentially it walks the matrix in a Z-fashion.
+##  
+## mat = 
+##   1   4   7
+##   2   5   8
+##   3   6   9
+## then zigzag(mat) gives the output,
+## [1   2   4   7   5   3   6   8   9], by walking as
+## shown in the figure from pt 1 in that order of output.
+## The argument @var{mtrx} should be a MxN matrix 
+##
+## An example of zagzig use:
+## @example
+## @group
+## mat = reshape(1:9,3,3);
+## zigzag(mat)
+## ans =[1   2   4   7   5   3   6   8   9]
+##
+## @end group
+## @end example
+##
+## @end deftypefn
+## @seealso{zagzig}
+
+## Author:   Fredrik Bulow, <fredrik.bulow@gmail.com>
+
+function rval = zigzag(mtrx)
+  if nargin < 1
+    error('usage: zigzag(matrix); see help zigzag');
+  end
+  n=size(mtrx);
+  
+  if(issquare(mtrx)) #Square matrix (quick case)
+
+    ##We create a matrix of the same size as mtrx where odd elements are
+    ##1, others 0.
+    odd=kron(ones(ceil(n/2)),eye(2))((1:n(1)),(1:n(2)));
+
+    ##We transpose even elements only.
+    mtrx = mtrx.*odd + (mtrx.*(1-odd))';
+
+    ##Now we mirror the matrix. The desired vector is now the
+    ##concatenation of the diagonals.
+    mtrx=mtrx(:,1+size(mtrx,2)-(1:size(mtrx,2)));
+
+    ##Picking out the diagonals.
+    rval  = [];
+    for i = n(2)-1:-1:1-n(1)
+      rval=[rval diag(mtrx,i)'];
+    endfor
+
+  else #Not square (Slow cases)
+    mtrx=mtrx(:,1+size(mtrx,2)-(1:size(mtrx,2)));
+
+    ##Picking out the diagonals and reversing odd ones manually.
+    rval  = [];
+    for i = n(2)-1:-1:1-n(1)
+      new = diag(mtrx,i);
+      if(floor(i/2)==i/2) ##Even?
+	rval=[rval new'];
+      else                ##Odd!
+	rval=[rval new((1+length(new))-(1:length(new)))'];
+      endif
+    endfor
+  endif
+endfunction
+%!
+%!assert(zigzag(reshape(1:9,3,3)),[1   2   4   7   5   3   6   8   9])
+%!