efficient way manipulating 3 dimensional array to 2 dimensional array.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Suppose that A is n_1 by n_1 symmetric matrix, and B is n_2 by n_2 symmetric matrix, where typically n_1 > n_2 and n_1 is from 10^3 to 10^5. I would like to get the following (n_1*n_2) by (n_1*n_2) matrix C such that each block of C is C_{ij}=\exp(A\text{.^}2/B_{i,j}^{1.5})/B_{i,j} with i=1, ..., n_2; j=1,..., n_2.
I got two ways to compute this in MATLAB, but both methods do not give me satisfactory timing. In the following I will give a minimal example in MATLAB code.
n1 = 400; n2 = 15;
A = randn(n1); A = A + A' + 10*eye(n1);
B = randn(n2); B = B + B' + 5*eye(n2);
One way:
tic;
Atemp = repmat(A, n2, n2);
Btemp = kron(B, ones(n1));
C1 = exp(Atemp.^2./Btemp.^1.5)./Btemp;
toc;
Elapsed time is 2.402167 seconds.
Another way:
tic;
Btemp = reshape(B, [1 1 n2*n2]);
Ctemp = bsxfun(@(x,y) exp(x.^2/y.^1.5)/y, A, Btemp);
[a, b, c] = size(Ctemp);
Ctemp = reshape(mat2cell(Ctemp, a, b, ones(c,1)), sqrt(c), sqrt(c));
C2 = cell2mat(Ctemp);
toc;
Elapsed time is 2.923428 seconds.
I am wondering whether there are more efficient way to get the matrix C in MATLAB? The resulting matrix C is required for cholesky decomposition.
Thank you very much!
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!