Effacer les filtres
Effacer les filtres

Page-wise Diagonalization?

49 vues (au cours des 30 derniers jours)
Ty Clements
Ty Clements le 28 Juin 2023
Réponse apportée : Matt J le 3 Juil 2023
Does anyone know a fast way to do a page-wise diagonalization? I have an array A of size [n,1,m] and want an array B of size [n,n,m] where the diagonals of B are the orignal vectors of A. I know I can call diag(A(:,1,i)) in a loop but that is time inefficent, and I can't use pagefun because the school I get my liscense from blocks it for some reason. Any good ideas on how to do this?

Réponse acceptée

DGM
DGM le 29 Juin 2023
This isn't much, but it's one idea. The meager speed advantage falls off for small inputs. diag() is pretty fast to begin with, but I'm not the clever one around here,
n = 1000;
m = 1000;
A = (1:n).' + 10*permute(0:m-1,[1 3 2]); % test array
% using diag() in a loop
tic
B0 = zeros(n,n,m);
for k = 1:m
B0(:,:,k) = diag(A(:,1,k));
end
toc
Elapsed time is 3.228410 seconds.
% doing a thing with linear indexing
tic
idx = (1:n+1:n^2).' + n^2.*(0:m-1);
B = zeros(n,n,m);
B(idx) = A;
toc
Elapsed time is 1.190878 seconds.
isequal(B,B0)
ans = logical
1
  1 commentaire
Ty Clements
Ty Clements le 3 Juil 2023
Thank you. This works the best because I didn't say it, but the nondiagonal elements of B couldn't be changed, as I was using their columns in the compuation and already set. I was just trying to minimize the time as far as I could for the function for an optimization algorithm's fitness funtion.

Connectez-vous pour commenter.

Plus de réponses (2)

Ram Sreekar
Ram Sreekar le 29 Juin 2023
Hi Ty Clements,
As per my understanding, you want to perform page-wise diagonalization of a matrix ‘A’ of dimension [n, 1, m] such that the resulting matrix ‘B’ (say) is of dimension [n, n, m] where every B(:, :, i) is a diagonal matrix such that the diagonal elements are vectors A(:, :, i) for all i=1 to m.
You can refer to the example code given below.
% Example input array A of size [n,1,m]
A = rand(3, 1, 2);
% Get the size of A
[n, ~, m] = size(A);
% Create a 3D identity matrix of size [n, n, m]
I = repmat(eye(n), 1, 1, m);
% Perform element-wise multiplication between A and I using bsxfun
B = bsxfun(@times, A, I);
% Reshape B to size [n, n, m]
B = reshape(B, n, n, m);
Here, the ‘repmat’ function is used to create a matrix I of dimension [n, n, m] where every I(:, :, i) for i=1 to m is an Identity matrix.
The ‘bsxfun’ function is used to perform element-wise multiplication of A and I.
You can refer to links given below for detailed explanation of both the functions ‘repmat’ and ‘bsxfun’.
Hope this helps you resolve your query.
  1 commentaire
DGM
DGM le 29 Juin 2023
If you're using bsxfun(), there's no need to use repmat; also, there's no need to reshape the result.
n = 1000;
m = 100;
A = randi(9,n,1,m);
tic
% original method
B0 = zeros(n,n,m);
for k = 1:m
B0(:,:,k) = diag(A(:,1,k));
end
toc
Elapsed time is 0.328146 seconds.
tic
% Create a 3D identity matrix of size [n, n, m]
I = repmat(eye(n), 1, 1, m);
% Perform element-wise multiplication between A and I using bsxfun
B1 = bsxfun(@times, A, I);
% Reshape B to size [n, n, m]
B1 = reshape(B1, n, n, m);
toc
Elapsed time is 0.342850 seconds.
tic
% cuts the time in half
B2 = bsxfun(@times, A, eye(n));
toc
Elapsed time is 0.143244 seconds.
tic
% implicit array expansion works without bsxfun() since R2016b
B3 = A.*eye(n);
toc
Elapsed time is 0.156168 seconds.
isequal(B1,B0)
ans = logical
1
isequal(B2,B0)
ans = logical
1
isequal(B3,B0)
ans = logical
1

Connectez-vous pour commenter.


Matt J
Matt J le 3 Juil 2023
A=rand(4,1,3); pagetranspose(A)
ans =
ans(:,:,1) = 0.0282 0.8509 0.5499 0.7632 ans(:,:,2) = 0.3504 0.5504 0.0568 0.5077 ans(:,:,3) = 0.1700 0.2185 0.4936 0.1387
[n,~,p]=size(A);
B=zeros(n^2,p);
B(1:n+1:end,:)=reshape(A,n,p);
B=reshape(B,n,n,p)
B =
B(:,:,1) = 0.0282 0 0 0 0 0.8509 0 0 0 0 0.5499 0 0 0 0 0.7632 B(:,:,2) = 0.3504 0 0 0 0 0.5504 0 0 0 0 0.0568 0 0 0 0 0.5077 B(:,:,3) = 0.1700 0 0 0 0 0.2185 0 0 0 0 0.4936 0 0 0 0 0.1387

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by