random matrix with specific condition in matlab ?
Show older comments
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 ]
Answers (2)
Azzi Abdelmalek
on 11 Apr 2016
B=randi([0 1],size(A))
B(:,1)=A(:,1)
B(2,:)=A(2,:)
4 Comments
Firas Al-Kharabsheh
on 11 Apr 2016
Azzi Abdelmalek
on 11 Apr 2016
Not clear
Firas Al-Kharabsheh
on 11 Apr 2016
Edited: Firas Al-Kharabsheh
on 11 Apr 2016
Azzi Abdelmalek
on 11 Apr 2016
[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
on 11 Apr 2016
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, :) = []
Categories
Find more on Creating and Concatenating Matrices 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!