For loop over function handle, how to speed up the code?

3 visualizaciones (últimos 30 días)
Sina
Sina el 13 de Sept. de 2022
Comentada: Sina el 14 de Sept. de 2022
Hi all,
I have a high-dimensional operator in form of a function handle which I can see as a matrix. For some reason I want this matrix as if its columns are normalized, but because of some randomization occuring within the operator I cannot do the normalization inside the function. So my idea was to input canonical vectors where (vectors with zero everywhere and 1 at one of the entries) to the operator, retrieve each column of the matrix ( is the ith column of A), and store the norms so that I'd be able to use it later in my code, but this takes too much time because N (the dimension of the rowspace of the matrix) is very high (i.e 2^16). On the other hand I cannot give an identity matrix as the input to have the matrix all at once, because my matrix is not sparse and thus requires too much space (nearly 32 GB). Here's the code:
col_norms = zeros(N, 1); % column norms to be stored
z = zeros(N, 1);
z(1) = 1;
z = sparse(z);
col_norms(1) = norm(A(z)); % A is a function handle (the matrix)
for i = 1:N-1
z = circshift(z, i); % shifting the current vector to produce the next canonical vector
col_norms(i + 1) = norm(A(z));
end
How can I speed up this procedure? Is there any better way of finding the column norms of my operator
? Any help would be appreciated.
  1 comentario
Jan
Jan el 13 de Sept. de 2022
Use the profiler to find the bottleneck. Most likely it is not found in the posted code, but in the code of A(z).

Iniciar sesión para comentar.

Respuestas (1)

Bruno Luong
Bruno Luong el 13 de Sept. de 2022
Editada: Bruno Luong el 13 de Sept. de 2022
If your handle A can accept a matrix as input and the calculation of A(B) is not bottleneck, you can work by chunk
% N = 2^16;
chunk=128; % Adjust to your RAM available
col_norm = zeros(N,1);
ndone = 0;
while ndone < N
i = ndone+1:min(ndone+chunk,N);
j = i-ndone;
E = sparse(i,j,1,N,j(end));
col_norm(i) = sqrt(sum(A(E).^2,1));
ndone = i(end);
end
  3 comentarios
Bruno Luong
Bruno Luong el 14 de Sept. de 2022
Editada: Bruno Luong el 14 de Sept. de 2022
You can easily "overload" it
% N = 2^16;
chunk=128; % Adjust to your RAM available
col_norm = zeros(N,1);
ndone = 0;
while ndone < N
i = ndone+1:min(ndone+chunk,N);
j = i-ndone;
E = sparse(i,j,1,N,j(end));
col_norm(i) = sqrt(sum(AMat(E).^2,1));
ndone = i(end);
end
function AX = AMat(X)
for k=size(X,2):-1:1
%AX(:,k) = full(A(X(:,k)));
AX(:,k) = A(X(:,k));
end
end
But I'am afraid the bottleneck is the matrix-vector handle, not the wraparound that is matter. So we can't help you.
Sina
Sina el 14 de Sept. de 2022
Yes, you're right. It seems that I arrived at a deadend here. Maybe I edit the question in the next few days with more details of the function handle.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by