Make entries of first row and column of matrix all equal to zero.

91 次查看(过去 30 天)
I'm trying to create an nxn matrix that has all entries in the first row and column equal to zero, and also all of the diagonal entries need to be equal to zero. The other entries are random 1's and 0's. I have so far made the diagonals equal to zero but am struggling with making the first row and column equal to zero. Below is what I have so far:
n = 10;
B = randi([0,1],n,n);
v = zeros(1,n);
B = B + diag(v-diag(B));
Thanks

回答(2 个)

Fabio Freschi
Fabio Freschi 2020-1-19
% nullify first col
B(:,1) = 0;
%nullify first row
B(1,:) = 0;

dpb
dpb 2020-1-19
编辑:dpb 2020-1-20
Modestly different approach...instead of having to clear row/column, just don't write into them to start with:
N=10; % Final array size
M(2:N,2:N)=randi([0 1],N-1); % Fill lower RH block
M(1:N+1:end)=0; % Clear diagonal

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by