# Generate ones and zeros based on size of each element in a matrix nxn

1 view (last 30 days)
Andreas Aristotelous on 26 Mar 2023
i have a matrix a= [3 4 6; 0 2 3; 4 5 6] and i want to create a new matrix with ones and zeros like this:
one_zero= [ 1 1 1 0 0 0 0 1 1 1 1 1 1; 1 0 0 1 1 1; 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1]
in order to match the gaps for the one_zero matrix to have nxn columns, i want to fill gaps with 0 ( when for example the sum of first row does not match with the sum of the second row)
How it can be done?

Dyuman Joshi on 26 Mar 2023
Edited: Dyuman Joshi on 26 Mar 2023
The output corresponding to 0 in a (i.e. a(2,1)) is a single 1. If that is the desired output, you will have to replace all the zeros in the matrix with one.
a = [3 4 6; 0 2 3; 4 5 6];
a(a==0)=1;
%sum of each row
m = sum(a,2);
s = size(a);
%preallocating the output matrix
out = zeros(s(1),max(m));
%vector for repetition
vec=rem(1:s(2),2);
for k=1:s(1)
out(k,1:m(k))=repelem(vec,a(k,:));
end
out
out = 3×15
1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1
Andreas Aristotelous on 26 Mar 2023
Thank you again @Dyuman Joshi. I really appreciate your help !