Sparse matrix with diagonal zero and elements in every row/column
이전 댓글 표시
I'm trying to produce a sparse matrix that has zeros on the diagonal, but also has at least one element in each row and column.
With this code I get the first part, but I don't know how to make sure that each row and column have elements in them.
R = sprand(4,4,0.5);
r = 0 + (0.5 - 0)*rand(4,4);
A = full (R);
A(1:4+1:4*4)=0;
Nonz = nnz(A);
Diff = ceil(0.5*(4^2-4)) - Nonz;
B = find (A==0);
A=A';
B = find (A==0);
C = setdiff(B,1:4+1:4*4);
D = datasample(C,Diff,1,'Replace',false);
E=A;
for i = 1:length(D)
E(D(i)) = 1;
end
E=E';
E(find(E)) = r(find(E));
댓글 수: 2
I'm trying to produce a sparse matrix that has zeros on the diagonal, but also has at least one element in each row and column ...
You already have that!?
>> A = 0 + (0.5 - 0)*rand(4,4)
A(1:4+1:4*4)=0
A =
0.2947 0.0229 0.1600 0.4296
0.4233 0.4000 0.4959 0.2969
0.0890 0.3980 0.2755 0.4307
0.1483 0.1979 0.0073 0.3885
A =
0 0.0229 0.1600 0.4296
0.4233 0 0.4959 0.2969
0.0890 0.3980 0 0.4307
0.1483 0.1979 0.0073 0
Evelyn Salt
2017년 8월 18일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!