Filter löschen
Filter löschen

Page-wise Diagonalization?

49 Ansichten (letzte 30 Tage)
Ty Clements
Ty Clements am 28 Jun. 2023
Beantwortet: Matt J am 3 Jul. 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?

Akzeptierte Antwort

DGM
DGM am 29 Jun. 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 Kommentar
Ty Clements
Ty Clements am 3 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Ram Sreekar
Ram Sreekar am 29 Jun. 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 Kommentar
DGM
DGM am 29 Jun. 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

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 3 Jul. 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

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by