How to generate a random matrix with conditions?

How to generate a matrix ( n x m ) where i need this number represent a number of ones in each row like this
A = [ 2 3 1
3 1 1
4 1 0
2 2 0 ]
I need to generate a matrix like this
X = [ 1 1 0 1 1 1 0 1
1 1 1 0 1 0 0 1
1 1 1 1 0 0 1 0
0 1 1 0 0 0 1 1 ]
Where every numbers in matrix A must be at least zero between its numbers of one

1 commentaire

It sounds like the poster wants something like run-length decoding but where only the length of the runs of 1's are given and it's assumed there are 0's between those runs. But you're right, the poster needs to clarify the rules for how many 0's should be between the runs. It's not just one 0 between each run, as seen in rows 2, 3, and 4.

Connectez-vous pour commenter.

Réponses (3)

Azzi Abdelmalek
Azzi Abdelmalek le 8 Avr 2016
Modifié(e) : Azzi Abdelmalek le 8 Avr 2016
It's almost what you are asking for!
A = [ 2 3 1
3 1 1
4 1 0
2 2 0 ]
[n,m]=size(A);
ii=max(sum(A,2));
no=ii+m-1;
out=zeros(n,no);
for k=1:n
r=A(k,:);
nz=no-sum(r);
q=num2cell(zeros(1,m-1));
idx=randi(m-1,1,nz-m+1);
for kk=1:numel(idx)
q{idx(kk)}(end+1)=0;
end
aa=[];
for pp=1:m-1
aa=[aa ones(1,r(pp)) q{pp}];
end
aa=[aa ones(1,r(end))];
out(k,:)=aa;
end
out
[m,n] = size(A);
X = zeros(m,sum(A,2) + n - 1);
a = arrayfun(@(x)ones(1,x),A,'un',0);
Xc(:,1:2:2*n-1) = a ;
Xc(:,2:2:end) = {0};
for ii = 1:m
b = [Xc{ii,:}];
X(ii,1:numel(b)) = b;
end
Azzi Abdelmalek
Azzi Abdelmalek le 8 Avr 2016
Modifié(e) : Azzi Abdelmalek le 8 Avr 2016
I think this is What you want:
A = [ 2 3 1
3 1 1
4 1 0
2 2 0 ]
[n,m]=size(A);
ii=max(sum(A,2));
no=ii+m-1;
out=zeros(n,no);
for k=1:n
r=A(k,:);
nz=no-sum(r);
q=[ cell(1) num2cell(zeros(1,m-1)) cell(1)];
idx=randi(m+1,1,nz-m+1);
for kk=1:numel(idx)
q{idx(kk)}(end+1)=0;
end
aa=q{1};
for pp=1:m
aa=[aa ones(1,r(pp)) q{pp+1}];
end
out(k,:)=aa;
end
out

11 commentaires

i want in each run give me another solution like this
first generation out = [ 1 1 0 1 1 1 0 1
1 1 1 0 1 0 1 0
1 1 1 1 0 1 0 0
1 1 0 1 1 0 0 0 ]
second generation out =[ 1 1 0 1 1 1 0 1
0 1 1 1 0 1 0 1
0 1 1 1 1 0 1 0
0 1 1 0 1 1 0 0]
third generation out = [ 1 1 0 1 1 1 0 1
0 1 1 1 0 1 0 1
0 1 1 1 1 0 0 1
0 0 1 1 0 0 1 1 ]
when i create a large matrix A there is may be more then one zeros between number like the third generation in last row
Azzi Abdelmalek
Azzi Abdelmalek le 8 Avr 2016
Modifié(e) : Azzi Abdelmalek le 8 Avr 2016
Ok, have you tested my code? What is the problem with it? You said it's random, then why you are asking for specific result?
when i run the code in every time the same result appear ? why give me the same result???
Azzi Abdelmalek
Azzi Abdelmalek le 8 Avr 2016
Modifié(e) : Azzi Abdelmalek le 8 Avr 2016
No, I am not getting the same result
First run
1 1 0 1 1 1 0 1
1 1 1 0 1 0 0 1
0 1 1 1 1 0 1 0
1 1 0 1 1 0 0 0
Second run
1 1 0 1 1 1 0 1
1 1 1 0 0 1 0 1
1 1 1 1 0 1 0 0
1 1 0 0 1 1 0 0
Third run
1 1 0 1 1 1 0 1
1 1 1 0 0 1 0 1
1 1 1 1 0 0 1 0
0 0 1 1 0 1 1 0
if i have this matrix for the column how can generate it like the row matrix
B = [ 0 1 2 1 represent the column number
3 2 3 2
1 2 1 1 ]
I didn't understand your question
How to generate a random matrix ( n x m ) where i need this number represent a number of ones in each column like this
B = [ 0 1 2 1
3 2 3 2
1 2 1 1 ]
I need to generate a matrix like this
y = [ 0 1 1 1
1 0 1 0
1 1 0 1
1 1 1 1
0 0 1 0
1 1 1 0
0 1 0 0
0 0 1 1 ]
Where every numbers in matrix A must be at least zero between its numbers of one
You can use the same code, instead of B, use the transpose of B, and transpose the result.
B = [ 0 1 2 1
3 2 3 2
1 2 1 1 ]
A=B'; % Transpose B
[n,m]=size(A);
ii=max(sum(A,2));
no=ii+m-1;
out=zeros(n,no);
for k=1:n
r=A(k,:);
nz=no-sum(r);
q=[ cell(1) num2cell(zeros(1,m-1)) cell(1)];
idx=randi(m+1,1,nz-m+1);
for kk=1:numel(idx)
q{idx(kk)}(end+1)=0;
end
aa=q{1};
for pp=1:m
aa=[aa ones(1,r(pp)) q{pp+1}];
end
out(k,:)=aa;
end
out=out' % Transpose the result
Firas Al-Kharabsheh
Firas Al-Kharabsheh le 9 Avr 2016
Modifié(e) : Firas Al-Kharabsheh le 9 Avr 2016
how can i combine the matrix A and Matrix B to generate the random Matrix ?
( Combine the two code to generate a random matrix )
You need to post another question.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by