constructing a difficult large matrix

3 ビュー (過去 30 日間)
ektor
ektor 2020 年 5 月 23 日
コメント済み: David Goodmanson 2020 年 6 月 3 日
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 件のコメント
per isakson
per isakson 2020 年 5 月 23 日
"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" ?
ektor
ektor 2020 年 5 月 23 日
Hi isakson,
It will be a scalar. So each element of a_i will be multiplied by the corresponding element of h to produce a scalar (like inner product). Sorry for not clarifying first.
When I say fast, I mean not time consuming.

サインインしてコメントする。

採用された回答

David Goodmanson
David Goodmanson 2020 年 5 月 23 日
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 件のコメント
ektor
ektor 2020 年 5 月 23 日
HI david,
Just a quick question: " There is one exra element at the beginning corresponding to row a_0 but you can always delete it. "
what does this mean?
David Goodmanson
David Goodmanson 2020 年 6 月 3 日
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.

サインインしてコメントする。

その他の回答 (1 件)

per isakson
per isakson 2020 年 5 月 23 日
編集済み: per isakson 2020 年 5 月 23 日
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 件のコメント
David Goodmanson
David Goodmanson 2020 年 5 月 24 日
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.
per isakson
per isakson 2020 年 5 月 24 日
Yes, I need a copy of Walters Mind Reading Toolbox :(.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by