constructing a difficult large matrix
显示 更早的评论
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
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
2020-5-23
采纳的回答
更多回答(1 个)
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
2020-5-23
Hello per,
could you explain the idea behind putting the result onto the diagonal of a matrix which is otherwise zero?
per isakson
2020-5-23
编辑:per isakson
2020-5-23
Do you ask for why or how? I answer both.
"[...] constructing a complicated matrix A. The matrix A is a diagonal matrix" but I'm still not sure exactly what OP asked for.
Internally, Matlab stores arrays as linear sequences of elements together with some metadata, including the size of the array. Matlab uses column-major (and C row-major). Accessing array elements can be done with subscripts or linear indexing. (The functions, sub2ind and ind2sub convert between the two.) 1:N+1:end is the linear indicies of the diagonal elements.
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
2020-5-24
Yes, I need a copy of Walters Mind Reading Toolbox :(.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!