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 ]

Respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 11 de Abr. de 2016
B=randi([0 1],size(A))
B(:,1)=A(:,1)
B(2,:)=A(2,:)

4 comentarios

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
Azzi Abdelmalek
Azzi Abdelmalek el 11 de Abr. de 2016
Not clear
Firas Al-Kharabsheh
Firas Al-Kharabsheh el 11 de Abr. de 2016
Editada: Firas Al-Kharabsheh el 11 de Abr. de 2016
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

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 11 de Abr. de 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, :) = []

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Abr. de 2016

Comentada:

el 11 de Abr. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by