Hmm, this doesn't happen in the developer sources. The attached version
seems to work for me in 3.2.4. Could you try that?

Søren

man, 29 11 2010 kl. 15:27 +0100, skrev [email protected]:
> Ok, sorry.
> 
> >>> combnk(1:5,3)
> error: number of columns must match (1 != 3)
> error: called from:
> error:   /H/octave/combnk.m at line 58, column 14
> error:   /H/octave/combnk.m at line 38, column 12
> 
> If k<3, it's allright:
> >>> combnk(1:5,2)
> 
> ans =
> 
>    1   2
>    1   3
>    1   4
>    1   5
>    2   3
>    2   4
>    2   5
>    3   4
>    3   5
>    4   5
> 
> Martin
> 
> 
> ----- Mail Original -----
> De: "Søren Hauberg" <[email protected]>
> À: "martin ecarnot" <[email protected]>
> Cc: [email protected]
> Envoyé: Lundi 29 Novembre 2010 15h05:21 GMT +01:00 Amsterdam / Berlin / Berne 
> / Rome / Stockholm / Vienne
> Objet: Re: [OctDev] Permutations
> 
> man, 29 11 2010 kl. 14:58 +0100, skrev [email protected]:
> > Thanks for your reply and the script.
> > However, it doesn't work for me if k>2 (Octave 3.2.4)
> 
> Could you send me an example where it fails (including data, how you
> calling the function and the output you got) ?
> 
> Søren
> 

## Copyright (C) 2010  Soren Hauberg
##
## 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 <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {...@var{c} =} combnk (@var{data}, @var{k})
## Return all combinations of @var{k} elements in @var{data}.
## @end deftypefn

function retval = combnk (data, k)
  ## Check input
  if (! isvector (data))
    error ("combnk: first input argument must be a vector");
  endif 

  if (!isreal (k) || k != round (k) || k < 0)
    error ("combnk: second input argument must be a non-negative integer");
  endif

  ## Simple checks
  n = numel (data);
  if (k == 0 || k > n)
    retval = resize (data, 0, k);
  elseif (k == n)
    retval = data (:).';
  else
    retval = __combnk__ (data, k);
  endif

  ## For some odd reason Matlab seems to treat strings differently compared to other data-types...
  if (ischar (data))
     retval = flipud (retval);
  endif
endfunction

function retval = __combnk__ (data, k)
  ## Recursion stopping criteria
  if (k == 1)
    retval = data (:);
  else
    ## Process data
    n = numel (data);
    retval = [];
    for j = 1:n
      C = __combnk__ (data ((j+1):end), k-1);
      C = cat (2, repmat (data (j), rows (C), 1), C);
      if (!isempty (C))
        retval = [retval; C];
      endif
    endfor
  endif
endfunction

%!demo
%! c = combnk (1:5, 2);
%! disp ("All pairs of integers between 1 and 5:");
%! disp (c);

%!test
%! c = combnk (1:3, 2);
%! assert (c, [1, 2; 1, 3; 2, 3]);

%!test
%! c = combnk (1:3, 6);
%! assert (isempty (c));

%!test
%! c = combnk ({1, 2, 3}, 2);
%! assert (c, {1, 2; 1, 3; 2, 3});

%!test
%! c = combnk ("hello", 2);
%! assert (c, ["lo"; "lo"; "ll"; "eo"; "el"; "el"; "ho"; "hl"; "hl"; "he"]);
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Octave-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to