# efficient way manipulating 3 dimensional array to 2 dimensional array.

1 visualizzazione (ultimi 30 giorni)
Bayes il 16 Apr 2016
Modificato: Bayes il 17 Apr 2016
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by