1.随机生成仅仅有0。1元素的矩阵(m行n列)
A=round(rand(m,n))
2.随机生成每行有若干个0,1元素的矩阵,比方每行仅仅有2个元素为1,其它元素为0
A=zeros(4,5)
for i=1:4
A(i,:)=randperm(5);
for j=1:5
if A(i,j)<=2
A(i,j)=1;
else
A(i,j)=0;
end
end
end
A
输出:
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
A =
0 0 1 1 0
0 1 1 0 0
1 0 1 0 0
0 0 1 1 0
A=round(rand(m,n))
2.随机生成每行有若干个0,1元素的矩阵,比方每行仅仅有2个元素为1,其它元素为0
A=zeros(4,5)
for i=1:4
A(i,:)=randperm(5);
for j=1:5
if A(i,j)<=2
A(i,j)=1;
else
A(i,j)=0;
end
end
end
A
输出:
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
A =
0 0 1 1 0
0 1 1 0 0
1 0 1 0 0
0 0 1 1 0
请发表评论