constructing a difficult large matrix
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ektor
am 23 Mai 2020
Kommentiert: David Goodmanson
am 3 Jun. 2020
Dear all,
I am interested in constructing a complicated matrix A.
The matrix A is a diagonal matrix, where each diagonal element 'a_i' is of 1X1000, having the following patter
a_1=[p 1 0 0 0 0 0 .....0 ];
a_2=[p^2 p 1 0 0 0..... 0];
a_2=[p^3 p^2 p 1 0 0......0];
a_2=[p^4 p^3 p^2 p 1 0 0......0 ];
a_2=[p^5 p^4 p^3 p^2 p 1 0 ...0];
.
.
.
a_1000=[p^1000 p^999 p^998.........1];
And I want to multiply each element "a_i" with the vector h, where h is of dimension 1000X1, which contains just numbers of no particular pattern
Is there any way of doing that fast?
Maybe by using sparse or speye?
Thank you
2 Kommentare
per isakson
am 23 Mai 2020
"multiply each element "a_i" with the vector h, where h is of dimension 1000X1" the product will that be a scalar, vector or matrix?
"doing that fast" how fast is "fast" ?
Akzeptierte Antwort
David Goodmanson
am 23 Mai 2020
Hi ektor,
I presume you are interested not so much in the in the matrix A as in the 1000 scalars you get after you multiply each row of A by the vector h. To make things a little easier I included an extra row a_0 at the beginning.
a_0=[1 0 0 0 0 0 0 .....0 ];
a_1=[p 1 0 0 0 0 0 .....0 ];
a_2=[p^2 p 1 0 0 0..... 0];
a_3=[p^3 p^2 p 1 0 0......0];
a_4=[p^4 p^3 p^2 p 1 0 0......0 ];
a_5=[p^5 p^4 p^3 p^2 p 1 0 ...0];
.
.
a_1000=[p^1000 p^999 p^998.........1];
then
Ah = filter(1,[1 -p],h)
is the same as A*h. It's very fast. There is one exra element at the beginning corresponding to row a_0 but you can always delete it.
5 Kommentare
David Goodmanson
am 3 Jun. 2020
Hi ektor,
I meant that your matrix has 1000 rows and 1001 columns. The output is the inner product of h with each row, so 1000 values in all. However, for the filter function to reproduce that result, it was convenient to add a row at the top that I called a0. Then there are 1001 rows and 1001 values in the result. But the first result is due to the added row, so if you delete it you are back to the 1000 values in the original problem.
Weitere Antworten (1)
per isakson
am 23 Mai 2020
Bearbeitet: per isakson
am 23 Mai 2020
Here are three functions, two of which are based on the answer of David Goodmanson. I think all of them are fast enough. However, more important than speed is that their results are correct. More test are needed.
A small test with numbers that I'm able to check.
>> cssm(8)
ans =
3 0 0 0 0 0 0 0
0 7 0 0 0 0 0 0
0 0 15 0 0 0 0 0
0 0 0 31 0 0 0 0
0 0 0 0 63 0 0 0
0 0 0 0 0 127 0 0
0 0 0 0 0 0 255 0
0 0 0 0 0 0 0 510
>>
>> tic,cssm(1e4);toc
Elapsed time is 1.155732 seconds.
>> tic,M=dgsp(1e4);toc
Elapsed time is 0.490225 seconds.
>> tic,M=dg(1e4);toc
Elapsed time is 0.014082 seconds.
function M = cssm( N )
%%
h = ones( N, 1 );
p = 2;
pr = 1;
a = zeros( N, N );
a( N+1 : (N+1) : end ) = 1;
for jj = 1 : N
pr = pr * p;
a( jj : (N+1) : end-(jj-2)*N ) = pr;
end
M = zeros( N, N );
for jj = 1 : N
M(jj,jj) = a(jj,:) * h;
end
end
function M = dgsp( N )
%%
h = ones( N, 1 );
tmp = [ 1; h ];
p = 2;
%%
Ah = filter( 1, [1,-p], tmp );
M = sparse( zeros( N, N ) ); % better: sparse(10,10,0);
M( 1 : N+1 : end ) = Ah( 2 : end );
end
function M = dg( N )
%%
h = ones( N, 1 );
tmp = [ 1; h ];
p = 2;
%%
Ah = filter( 1, [1,-p], tmp );
M = zeros( N, N );
M( 1 : N+1 : end ) = Ah( 2 : end );
end
4 Kommentare
David Goodmanson
am 24 Mai 2020
I'm fine with the how, I was inquiring about the why, and now I see the direction you took. In the statement "A is a diagonal matrix, where each diagonal element 'a_i' is of 1X1000", if A is interpreted to be a very large block diagonal matrix, you do end up with the large diagonal matrix you have.
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!