5

I am looking for a simple way to get all combinations of an binary matrix. I tried already the function perms() but did not get a proper result.

I have for example an matrix N x N filled up with 1 and -1. With N=2 there would be 2^4 possible combinations of 1 and -1 like

       (1 1)          (1  1)          (-1 -1)
M(1) = (1 1) , M(2) = (1 -1) , M(3) = ( 1  1) and so on...

When I use perms() I don't get for example the first matrix.

How can I fix that?

3 Answers 3

5

You can represent all numbers between 0 and 2^(N^2)-1 as binary numbers, and then reshape:

N = 2;
v = (1:2^(N^2))-1;
A = dec2bin(v)' - '0'; %'// Or use: decimalToBinaryVector(v)';
A(A==0) = -1;
A = reshape(A,N,N,2^(N^2));
Sign up to request clarification or add additional context in comments.

7 Comments

Seems good to me,except the typo on first line (2^N-1 instead of 2^N²-1). Plus I'd point out that this approach doesn't force you to store all of these combinations in memory, as it gives a 1:1 correspondance between your matrices and 0:2^N²-1, meaning if you just need to get one of these matrices at random for example, you can just pick a number and then apply the equivalence
@NKN in your case you need to remove some duplicate matrices; the code gives a 2x2x24 matrix for A. (Seems like an easy fix to me). In case of this answer, no idea, it does indeed deliver you all 16 possibilities for a 2x2 matrix containing 1 and -1.
@NKN: in your answer, the number of 1s and -1s is fixed.
It seems that decimalToBinaryVector is specific to the Data Acquisition Toolbox. I believe you should be able to do the same with A=(dec2bin(v)-'0').'; though.
@AndrasDeak: I remembered there's a function to do it, but for some reason the search in Matlab's doc gave me decimalToBinaryVector. I'll edit my answer. Thanks!
|
-1

A simple hack is as follows:

v = [1 -1 1 -1];
P = perms(v);
for ii = 1:size(P,1)
    A = reshape(P(ii,:),2,2)
end

which results in:

A =

    -1    -1
     1     1

...

Still there are some identical matrices in the result which should be removed.

2 Comments

With your approach I do not get matrices with more than two 1 or -1.
You get only matrices with a combination of two -1s and two 1s. To remove the duplicates I suggest a line P = unique(P,'rows') to eliminate duplicates.
-1

I think that I found an solution to my problem

L = 2;
N = L^2;
v = cell(N,1);
for k = 1:N
    v{k} = linspace(-1,1,2);
end

ne=numel(v);
x=cell(ne,1);
[x{1:ne,1}]=ndgrid(v{end:-1:1});
p=reshape(cat(ne+1,x{:}),[],ne);

F = cell(length(p),1);
for k=1:length(p)
    F{k} = reshape(p(k,:),L,L);
end

5 Comments

Two things: please do not use clear all in SO codes, as people tend to copy these codes in their own and a clear call messes up everything. Second: p is a 65536x16 double, which seems a bit large for your permutations. All desired permutations are there, but way too often. You went a bit overboard with your dimensions I think.
Also: v is just a 16x1 cell with 16 times the same matrix: [-1 1]. That entire loop is just a difficult way of writing those two numbers. (As a side note, I did not vote on this answer)
I did downvote the answer now, because whilst you did remove the clear, that was not the main point of this solution being wrong. Just run it and see for yourself that v contains 16 matrices all being [-1 1], and p is way larger than you'd need. This makes the answer unusable.
@Adriaan Now, it should be right. Can you have a look?
It's correct now, but you still have that weird assignment for v, see a previous comment as explanation. This results in all cells of x containing duplicate matrices, making this code not very efficient. Also I'd suggest not using a cell for F, but a 3D array as such: F(:,:,k) = reshape(p(k,:),L,L); All in all it's working, but not pretty nor efficient. I removed the downvote though, as it does work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.