From: Vinh Le on 20 Mar 2010 19:02 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 ==============================================
From: Vinh Le on 20 Mar 2010 19:10 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
From: Vinh Le on 20 Mar 2010 19:16 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
From: Vinh Le on 20 Mar 2010 19:32 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
From: Vinh Le on 20 Mar 2010 19:36
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 |