1

I'm trying to permute a cell of strings and get the unique rows back. Example: I'm giving four values and it permutes those values and gives those values back in rows of two. This works below but I notice the rows repeat in some areas. I tried adding the unique command out=unique(perms(A),'rows') but that comes back with an error.

clear all
more off
A={'(+)sig','(-)sig','(+)flip','(-)flip'}
out=perms(A);

for n=1:length(out)
  %fprintf([num2str(n), ',', out{n,1},',',out{n,2},',',out{n,3},'\n'])
  fprintf([num2str(n), ',', out{n,1},',',out{n,2},'\n'])
end

Results:

1,(+)sig,(-)sig
2,(-)sig,(+)sig
3,(+)sig,(+)flip
4,(-)sig,(+)flip
5,(+)flip,(+)sig
6,(+)flip,(-)sig
7,(+)sig,(-)sig
8,(-)sig,(+)sig
9,(+)sig,(+)flip
10,(-)sig,(+)flip
11,(+)flip,(+)sig
12,(+)flip,(-)sig
13,(+)sig,(-)flip
14,(-)sig,(-)flip
15,(+)sig,(-)flip
16,(-)sig,(-)flip
17,(+)flip,(-)flip
18,(+)flip,(-)flip
19,(-)flip,(+)sig
20,(-)flip,(-)sig
21,(-)flip,(+)sig
22,(-)flip,(-)sig
23,(-)flip,(+)flip
24,(-)flip,(+)flip

The issue is that some of the values double up see below how can I have the rows be unique rows?

1,(+)sig,(-)sig
7,(+)sig,(-)sig
2,(-)sig,(+)sig
8,(-)sig,(+)sig 
etc..

Ps: I'm using Octave 4.0 which is like Matlab

Rick T
  • 3,349
  • 10
  • 54
  • 119
  • You are getting all permutations of 4 elements, and then taking the first two columns only - so the rows of `out` are unique, but the rows of the first two columns of `out` are not. Do you want all combinations of the 4 elements? – Wolfie Sep 06 '17 at 15:36

1 Answers1

3

Note: code below works for both MATLAB and Octave 4.0.

Your problem is that you're not looking at the entire content of out. The function perms creates a 24-by-4 cell array, not a 24-by-2. If you look at the entire row, you'll see that each row is a unique permutation:

A = {'(+)sig', '(-)sig', '(+)flip', '(-)flip'};
out = perms(A)

out =

  24×4 cell array

    '(-)flip'    '(+)flip'    '(-)sig'     '(+)sig' 
    '(-)flip'    '(+)flip'    '(+)sig'     '(-)sig' 
    '(-)flip'    '(-)sig'     '(+)flip'    '(+)sig' 
    '(-)flip'    '(-)sig'     '(+)sig'     '(+)flip'
    '(-)flip'    '(+)sig'     '(+)flip'    '(-)sig' 
    '(-)flip'    '(+)sig'     '(-)sig'     '(+)flip'
    '(+)flip'    '(-)flip'    '(-)sig'     '(+)sig' 
    '(+)flip'    '(-)flip'    '(+)sig'     '(-)sig' 
    '(+)flip'    '(-)sig'     '(-)flip'    '(+)sig' 
    '(+)flip'    '(-)sig'     '(+)sig'     '(-)flip'
    '(+)flip'    '(+)sig'     '(-)flip'    '(-)sig' 
    '(+)flip'    '(+)sig'     '(-)sig'     '(-)flip'
    '(-)sig'     '(-)flip'    '(+)flip'    '(+)sig' 
    '(-)sig'     '(-)flip'    '(+)sig'     '(+)flip'
    '(-)sig'     '(+)flip'    '(-)flip'    '(+)sig' 
    '(-)sig'     '(+)flip'    '(+)sig'     '(-)flip'
    '(-)sig'     '(+)sig'     '(-)flip'    '(+)flip'
    '(-)sig'     '(+)sig'     '(+)flip'    '(-)flip'
    '(+)sig'     '(-)flip'    '(+)flip'    '(-)sig' 
    '(+)sig'     '(-)flip'    '(-)sig'     '(+)flip'
    '(+)sig'     '(+)flip'    '(-)flip'    '(-)sig' 
    '(+)sig'     '(+)flip'    '(-)sig'     '(-)flip'
    '(+)sig'     '(-)sig'     '(-)flip'    '(+)flip'
    '(+)sig'     '(-)sig'     '(+)flip'    '(-)flip'

If you'd like to generate all of the permutations of 2 items chosen from 4 items, you can use nchoosek like so:

p = nchoosek(1:4, 2);  % Unique combinations of 2
p = [p; flip(p, 2)];   % Add flipped version for all permutations
out = A(p)

out =

  12×2 cell array

    '(+)sig'     '(-)sig' 
    '(+)sig'     '(+)flip'
    '(+)sig'     '(-)flip'
    '(-)sig'     '(+)flip'
    '(-)sig'     '(-)flip'
    '(+)flip'    '(-)flip'
    '(-)sig'     '(+)sig' 
    '(+)flip'    '(+)sig' 
    '(-)flip'    '(+)sig' 
    '(+)flip'    '(-)sig' 
    '(-)flip'    '(-)sig' 
    '(-)flip'    '(+)flip'
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • For OP/other readers, note there are [several methods for finding all combinations of 2 vectors](https://stackoverflow.com/questions/7446946/how-to-generate-all-pairs-from-two-vectors-in-matlab-using-vectorised-code) which could be used to generate `p` as seen in your last code block. – Wolfie Sep 06 '17 at 16:07