How can I replace the upper off diagonal(super diagonal) and lower off diagonal(sub diagonal) of a matrix?

조회 수: 20 (최근 30일)
Given a symmetric tridiagonal matrix T generated by
n=5;
p=1;
q=1.7;
r=1;
T=full(gallery('tridiagonal',n,p,q,r));
T=[1.8 1 0 0 0;1 1.8 0 0 0;0 1 1.8 1 0;0 0 1 1.8 1;0 0 0 1 1.8]
T = 5x5
1.8000 1.0000 0 0 0 1.0000 1.8000 0 0 0 0 1.0000 1.8000 1.0000 0 0 0 1.0000 1.8000 1.0000 0 0 0 1.0000 1.8000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
How do I change the 1 1 1 1 on both upper and lower off diagonal to 1 0 1 0? What if n is arbitrary, is there any code that fix the off diagonals to a desired vector?

답변 (5개)

the cyclist
the cyclist 2024년 8월 13일
% Arbitrary size
N=5;
% Example input
A = rand(N); % NxN random matrix
disp(A)
0.4869 0.1564 0.9023 0.9455 0.0501 0.2761 0.9904 0.0819 0.3533 0.5165 0.9278 0.9213 0.3445 0.1539 0.7664 0.6829 0.5433 0.1951 0.4712 0.8439 0.9469 0.5196 0.3641 0.8529 0.7284
% Vector to set as the sub- or superdiagonal
v = rand(N-1,1); % The length of v should be one less than the number of rows/columns in A
% Set the subdiagonal to the values in v
A(sub2ind(size(A), 2:N, 1:(N-1))) = v;
% Set the superdiagonal to the values in v
A(sub2ind(size(A), 1:(N-1), 2:N)) = v;
% Display the updated matrix
disp(A)
0.4869 0.6336 0.9023 0.9455 0.0501 0.6336 0.9904 0.7321 0.3533 0.5165 0.9278 0.7321 0.3445 0.0237 0.7664 0.6829 0.5433 0.0237 0.4712 0.3475 0.9469 0.5196 0.3641 0.3475 0.7284

David Goodmanson
David Goodmanson 2024년 8월 13일
편집: David Goodmanson 2024년 8월 13일
Hello Olawale
m = rand(5,5)
m1 = diag(m,1) % original upper diagonal
a = [2 3 4 5]' % new upper diagonal elements
mnew = m - diag(m1,1) + diag(a,1)
and the lower diagonal works the same way with 1 replaced by -1 everywhere.
  댓글 수: 1
John D'Errico
John D'Errico 2024년 8월 13일
Certainly the preferred solution almost always. Far cleaner. Easy on the eyes, to read, to debug. Code that you can follow is important one day in the future, when you will need to change it.
The only reason why an indexing solution would be preferred is if the matrices were large, AND if the time cost was significant, so you were doing this operation sufficiently often to matter.

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


Naga
Naga 2024년 8월 13일
Hello Olawale,
To modify the off-diagonal elements of the symmetric tridiagonal matrix T to a specified pattern, you can use indexing to directly set these values. Below is a MATLAB code snippet that demonstrates how to change the off-diagonal elements to 1 0 1 0 for the given matrix T.
for i = 1:length(desired_vector)
if i <= n-1
T(i, i+1) = desired_vector(i); % Upper off-diagonal
T(i+1, i) = desired_vector(i); % Lower off-diagonal
end
end

Matt J
Matt J 2024년 8월 13일
편집: Matt J 2024년 8월 13일
n=8;
T=diag(repelem(1.8,n));
T(2:2*(n+1):end)=1;
T(n+1:2*(n+1):end)=1
T = 8x8
1.8000 1.0000 0 0 0 0 0 0 1.0000 1.8000 0 0 0 0 0 0 0 0 1.8000 1.0000 0 0 0 0 0 0 1.0000 1.8000 0 0 0 0 0 0 0 0 1.8000 1.0000 0 0 0 0 0 0 1.0000 1.8000 0 0 0 0 0 0 0 0 1.8000 1.0000 0 0 0 0 0 0 1.0000 1.8000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Matt J
Matt J 2024년 8월 13일
편집: Matt J 2024년 8월 13일
n=8;
T=diag(rand(n,1))
T = 8x8
0.1935 0 0 0 0 0 0 0 0 0.7146 0 0 0 0 0 0 0 0 0.1206 0 0 0 0 0 0 0 0 0.4606 0 0 0 0 0 0 0 0 0.2613 0 0 0 0 0 0 0 0 0.9348 0 0 0 0 0 0 0 0 0.9571 0 0 0 0 0 0 0 0 0.3898
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
diagonal=diag(T);
offdiagonal=rand(n-1,1)' %desired values
offdiagonal = 1x7
0.7561 0.8615 0.9447 0.7633 0.1505 0.2376 0.3172
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B=[ [offdiagonal(:);0], [0;offdiagonal(:)] ];
T=full(spdiags(B,[-1,+1],T))
T = 8x8
0.1935 0.7561 0 0 0 0 0 0 0.7561 0.7146 0.8615 0 0 0 0 0 0 0.8615 0.1206 0.9447 0 0 0 0 0 0 0.9447 0.4606 0.7633 0 0 0 0 0 0 0.7633 0.2613 0.1505 0 0 0 0 0 0 0.1505 0.9348 0.2376 0 0 0 0 0 0 0.2376 0.9571 0.3172 0 0 0 0 0 0 0.3172 0.3898
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

카테고리

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