X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fmiscellaneous-1.1.0%2Fzigzag.m;fp=octave_packages%2Fmiscellaneous-1.1.0%2Fzigzag.m;h=956c638db0c017cb9aa5d69f8420ad3e5d5e6955;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/miscellaneous-1.1.0/zigzag.m b/octave_packages/miscellaneous-1.1.0/zigzag.m new file mode 100644 index 0000000..956c638 --- /dev/null +++ b/octave_packages/miscellaneous-1.1.0/zigzag.m @@ -0,0 +1,84 @@ +## Copyright (C) 2006 Fredrik Bulow +## +## 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 3 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, see . + +## -*- 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} + +function rval = zigzag(mtrx) + if nargin != 1 + print_usage; + endif + 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])