MATLAB: Create binary 0 and 1

0 and 1

Dear all,
I have matrix of binary with n=5
A=[0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
....
1 1 1 1 1]
but I only want to take a submatrix with 3 number 1 inside
subA=[ 0 1 1 0 1
1 0 1 0 1
0 1 1 1 0
........]
and
subB=[0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
0 0 0 1 1
.....
0 1 1 0 1
1 0 1 0 1
0 1 1 1 0
........]
. Couyld you help me? Thanks

Best Answer

subA = A(sum(A,2)==3,:);
For subB it is not entirely clear to me what you want. Do you want the rows of A arranged so that all rows with one 1 come first, then all rows with two 1's, then all rows with three 1's? Or what? E.g., this?
subB = [A(sum(A,2)==1,:);...
A(sum(A,2)==2,:);...
A(sum(A,2)==3,:);...
A(sum(A,2)==4,:);...
A(sum(A,2)==5,:)];