Thread Subject: Find all possible alternative arrays

Subject: Find all possible alternative arrays

From: Vinh Le

Date: 20 Mar, 2010 23:02:05

Message: 1 of 8

I have a binary matrix A (3x7):
 A =
 
           0 0 0 0 0 0 0
           0 0 0 1 1 1 1
           0 0 1 0 0 1 1

I would like to write a code to generate a matrix with all possible alternative of n element(s) in each row of A.

For example,
============================================
Row 1 of A is
           0 0 0 0 0 0 0
*if n = 1, then I will have matrixRow1
matrixRow1 =

       0 0 0 0 0 0 0
       0 0 0 0 0 0 1
       0 0 0 0 0 1 0
                                                   ........
       1 0 0 0 0 0 0
The result matrix S should have all possible alternative elements of all rows (24 by 8 matrix).
----------------------------------------------------------------------------------------------------
*if n = 2, the I will have matrixRow1 with BOTH 1 OR 2 elements changed
matrixRow1 =

       0 0 0 0 0 0 0
       0 0 0 0 0 0 1
       0 0 0 0 0 1 0
                                                   ........
       1 0 0 0 0 0 0
       0 0 0 0 1 1 0
       0 0 0 0 1 0 1
       0 0 0 0 1 1 0
                                                   ........
       1 1 0 0 0 0 0
==============================================

Subject: Find all possible alternative arrays

From: Vinh Le

Date: 20 Mar, 2010 23:10:05

Message: 2 of 8

sorry same typing errors
the first result matrix should be 24x7 in dimension
the first row has value 0 0 0 0 1 1 0 should be 0 0 0 0 0 1 1

Subject: Find all possible alternative arrays

From: Vinh Le

Date: 20 Mar, 2010 23:16:03

Message: 3 of 8

I tried the very small one - but it does not work
A = [ 0 0 0; 0 1 1; 1 1 1];
[r, c] = size(A);
for i=1:r
    row = A(i,:)
    for j=1:r
        addedRow = row;
        saveRow = row;
        row(1,j) = '1';
        row = saveRow
        resultMatrix = [row,addedRow];
    end
end
==============
really need assistance, please help

Subject: Find all possible alternative arrays

From: Vinh Le

Date: 20 Mar, 2010 23:32:04

Message: 4 of 8

I fixed my little code but still does not work well
================
A = [ 0 0 0; 0 1 1; 1 1 1]
[r, c] = size(A)
resultMatrix = []
for i=1:r
    row = A(i,:)
    for j=0:r-1
        saveRow = row
        if row(1,c-j) == 1
            row(1,c-j) = 0
        else
            row(1,c-j) = 1
        end
        addedRow = row
        row = saveRow
        resultMatrix = [resultMatrix;addedRow]
    end
end
=================
resultMatrix =

     0 0 1
     0 1 0
     1 0 0
     0 1 0
     0 0 1
     1 1 1
     1 1 0
     1 0 1
     0 1 1

should be
     0 0 1
     0 1 0
     1 0 0

Subject: Find all possible alternative arrays

From: Vinh Le

Date: 20 Mar, 2010 23:36:04

Message: 5 of 8

Sorry, the result matrix should be
resultMatrix =

     0 0 0
     0 0 1
     0 1 0
     1 0 0

     0 1 1
     0 1 0
     1 1 0
     1 0 1

     1 1 1
     1 1 0
     1 0 1
     0 1 1

Subject: Find all possible alternative arrays

From: Vinh Le

Date: 21 Mar, 2010 00:55:05

Message: 6 of 8

Almost get it for n = 1, try

A = [ 0 0 0 0 0 0 0;
           0 0 0 1 1 1 1;
           0 0 1 0 0 1 1;
           0 0 1 1 1 0 0;
           0 1 0 0 1 0 1;
           0 1 0 1 0 1 0;
           0 1 1 0 1 1 0;
           0 1 1 1 0 0 1;
           1 0 0 0 1 1 0;
           1 0 0 1 0 0 1;
           1 0 1 0 1 0 1;
           1 0 1 1 0 1 0;
           1 1 0 0 0 1 1;
           1 1 0 1 1 0 0;
           1 1 1 0 0 0 0;
           1 1 1 1 1 1 1]
       
       
[r, c] = size(A);
resultMatrix = [];
for i=1:r
    row = A(i,:);
    resultMatrix =[resultMatrix;row];
    for j=0:r-1
        saveRow = row;
        try
        if row(1,c-j) == 1
            row(1,c-j) = 0;
        else
            row(1,c-j) = 1;
        end
        addedRow = row;
        row = saveRow;
        resultMatrix = [resultMatrix;addedRow];
        catch
        end
    end
end


=====================

Subject: Find all possible alternative arrays

From: Matt J

Date: 21 Mar, 2010 12:52:04

Message: 7 of 8

"Vinh Le" <lekhanhvinh@gmail.com> wrote in message <ho3qp9$mqn$1@fred.mathworks.com>...
> Almost get it for n = 1, try
>
> A = [ 0 0 0 0 0 0 0;
> 0 0 0 1 1 1 1;
> 0 0 1 0 0 1 1;
> 0 0 1 1 1 0 0;

===================

Here's an automatic way to compute A for arbitrary number of colums and arbitrary n

m=6;
n=3;

idxc=nchoosek(1:m,n);
[p,q]=size(idxc);

idxr=repmat((1:p).',1,q);

A=false(p,m);

A( sub2ind([p,m],idxr,idxc) )=true,


 

Subject: Find all possible alternative arrays

From: Jos (10584)

Date: 21 Mar, 2010 20:41:04

Message: 8 of 8

"Vinh Le" <lekhanhvinh@gmail.com> wrote in message <ho3k5d$9g8$1@fred.mathworks.com>...
> I have a binary matrix A (3x7):

I am not sure I understand your problem completely, but perhaps PERMPOS might be useful here:

http://www.mathworks.com/matlabcentral/fileexchange/11216-permpos

A small example:

permpos(2,4)
% 1 1 0 0
% 1 0 1 0
% 1 0 0 1
% 0 1 1 0
% 0 1 0 1
% 0 0 1 1

hth
Jos

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com