# I want to create a binary matrix with a variable dimensions

4 views (last 30 days)

Show older comments

### Answers (1)

David Hill
on 11 Aug 2022

Edited: David Hill
on 11 Aug 2022

k=10;

M=50;

O=randi(M+1,k)-1;%number of ones per row (number of zeros per row= 50 - O)

for n=1:k

m=[ones(1,O(n)),zeros(1,M-O(n))];

Matrix(n,:)=m(randperm(M));

end

##### 2 Comments

David Hill
on 11 Aug 2022

You just need to clear Matrix or set Matrix = [ ] at the beginning.

k=10;

M=50;

Matrix=[];

O=randi(M+1,k)-1;%number of ones per row (number of zeros per row= 50 - O)

for n=1:k

m=[ones(1,O(n)),zeros(1,M-O(n))];

Matrix(n,:)=m(randperm(M));

end

### See Also

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!