random matrix with specific condition in matlab ?

how to generate a random matrix except some row or column ? like this
A = [ 1 0 0 0 0 0
1 1 0 1 1 1
0 0 0 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0 ]
I want to generate a random matrix where the first column and second row will be appear in the random matrix like this
A* = [ 1 0 0 1 0 1
1 1 0 1 1 1
0 0 1 1 0 1
1 0 1 0 0 0
1 1 0 1 1 0 ]

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 4월 11일

0 개 추천

B=randi([0 1],size(A))
B(:,1)=A(:,1)
B(2,:)=A(2,:)

댓글 수: 4

i want to do this by for loop because the matrix A will not be appear where the column or row may be different from the first column or the second row
Not clear
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 4월 11일
편집: Firas Al-Kharabsheh 2016년 4월 11일
in matrix A the row and column must determine by for loop because i need the code determine which column or row must be determine
[n,m]=size(A)
B=randi([0 1],n,m)
for k=1:n
if k==2
B(k,:)=A(k,:)
end
end
for k=1:m
if k==1
B(:,k)=A(:,k)
end
end

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2016년 4월 11일

0 개 추천

Try this (I hope it's not homework):
% Make A a random set of zeros and 1s.
A = randi([0, 1], 6, 6);
% Make column 3 all zeros.
A(:, 3) = 0;
% Make row 3 all zeros.
A(3, :) = 0
% Now find the all zero columns.
allZerosColumns = all(A == 0, 1)
% Remove those columns.
A(:, allZerosColumns) = []
% Now find the all zero rows.
allZerosRows = all(A == 0, 2)
% Remove those rows.
A(allZerosRows, :) = []

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2016년 4월 11일

댓글:

2016년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by