How can I creat a lower triangular with one vektor avoiding for loops?

3 visualizzazioni (ultimi 30 giorni)
Hello everybody,
i have one vektor (let's call it g) with a dimension N and I need to create a NxN lower triangular matrix where the first column is g from 1 to N,
the second column starts with a zero in the first row and then from second row starts again with g from 1 to (N-1)
the third column starts with zeros in the first and second row, followed by g from entry 1 to (N-2)...
and so on...
Example:
g=[1 2 3]
M=[1 0 0; 2 1 0; 3 2 1]
____
My Code right now has a for loop, however i need to avoid this.
function M=ltmatrix(g)
for a=1:lenght(g)
M(a:length(g),a)=g(1:length(g)+1-a);
end
end

Risposta accettata

John D'Errico
John D'Errico il 25 Mar 2020
Modificato: John D'Errico il 25 Mar 2020
Will it always be a matrix of that form? Or will you next ask how to fill the lower triangle with a fully general set of numbers? The reason I ask is because the solution would probably be different.
For example, I would not that your matrix ALWAYS has constant diagonals. The with G = [1 2 3], then we see that the main diagonal has a 1 on it, the first sub-diagonal is entirely 2. Then we see a 3 in the corner. Had N been larger, the behavior would have been the same.
But, IF indeed your goal is the simple behavior as you describe, then one simple line of code is sufficient.
G = 1:5;
A = tril(toeplitz(G',G))
A =
1 0 0 0 0
2 1 0 0 0
3 2 1 0 0
4 3 2 1 0
5 4 3 2 1
As you can see, no loops required.
However, suppose you had a much more general problem, where you wish to populate the elements of the lower triangle of a matrix with elements, perhaps these?
N = 5;
H = rand(1,N*(N+1)/2)
H =
Columns 1 through 9
0.55755 0.32534 0.7106 0.45013 0.93977 0.42243 0.97267 0.87573 0.9695
Columns 10 through 15
0.11099 0.97525 0.37793 0.16366 0.0897 0.72233
I'll stuff them into a triangular matrix in column-major order, which is how elements are stored in matrices in MATLAB.
However, even that code is almost as eay.
A = zeros(N,N);
A(logical(tril(ones(N)))) = H
A =
0.55755 0 0 0 0
0.32534 0.42243 0 0 0
0.7106 0.97267 0.11099 0 0
0.45013 0.87573 0.97525 0.16366 0
0.93977 0.9695 0.37793 0.0897 0.72233
As you can see, the elements are as I chose them to be.
The point of all this is to understand how MATLAB stores elements in memory, and then use that effectively.
  1 Commento
Tiliam
Tiliam il 25 Mar 2020
Thank you very much.
It is indeed that simple, so that i use your first given code.
It is required for a convolution problem, where i need to have a matrix with this style, because the goal is not to solve the convolution with a FFT or a Integral. I have to solve it via vector-matrix-multiplication, so i can do some "condtion work".
However: You helped me and it works. Thanks again!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by