How to generate lower triangle in a matrix where elements are the inverse of the upper traingle and diagonal is one
12 views (last 30 days)
Show older comments
Sanjeev Goyal
on 24 Apr 2017
Commented: Sanjeev Goyal
on 1 May 2017
I want to generate a matrix where lower triangle elements are the inverse of upper triangle elements and diagonal elements are one.
where user inputs the upper triangle elements and lower triangle elements are generated automatically. I have attached the required matrix as an example. Thanks!
0 Comments
Accepted Answer
James Tursa
on 24 Apr 2017
E.g.,
>> M = [0 7 9 8;
0 0 8 6;
0 0 0 4;
0 0 0 0]
M =
0 7 9 8
0 0 8 6
0 0 0 4
0 0 0 0
>> MT = (1./M)'
MT =
Inf Inf Inf Inf
0.1429 Inf Inf Inf
0.1111 0.1250 Inf Inf
0.1250 0.1667 0.2500 Inf
>> x = logical(tril(ones(size(M)),-1))
x =
0 0 0 0
1 0 0 0
1 1 0 0
1 1 1 0
>> M(x) = MT(x)
M =
0 7.0000 9.0000 8.0000
0.1429 0 8.0000 6.0000
0.1111 0.1250 0 4.0000
0.1250 0.1667 0.2500 0
>> M(1:size(M,1)+1:end) = 1
M =
1.0000 7.0000 9.0000 8.0000
0.1429 1.0000 8.0000 6.0000
0.1111 0.1250 1.0000 4.0000
0.1250 0.1667 0.2500 1.0000
5 Comments
James Tursa
on 28 Apr 2017
Edited: James Tursa
on 28 Apr 2017
E.g., here is some rough code that will do it for row order entry:
k = 1:10000;
k = (k.*(k+1))/2;
while( true )
disp('Enter the following vector inside of square brackets [ ]');
v = input('Input upper triangular (super-diagonal) elements in row order: ');
m = numel(v);
[a b] = ismember(m,k);
if( a )
break;
end
disp('*** Invalid number of elements to fill out a square matrix ***');
end
n = b + 1;
M = zeros(n,n);
x = logical(tril(ones(size(M)),-1));
M(x) = v;
M = M.';
MT = (1./M).';
M(x) = MT(x);
M(1:size(M,1)+1:end) = 1;
For other orders you can ask for more inputs and vary the mapping accordingly. E.g., entering the lower triangle part, or entering the numbers in column order.
More Answers (0)
See Also
Categories
Find more on Operating on Diagonal 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!