Generate ones and zeros based on size of each element in a matrix nxn
1 view (last 30 days)
Show older comments
Andreas Aristotelous
on 26 Mar 2023
Commented: 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?
0 Comments
Accepted Answer
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
3 Comments
More Answers (0)
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!