For loop over function handle, how to speed up the code?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Jan
2022 年 9 月 13 日
Use the profiler to find the bottleneck. Most likely it is not found in the posted code, but in the code of A(z).
回答 (1 件)
Bruno Luong
2022 年 9 月 13 日
編集済み: Bruno Luong
2022 年 9 月 13 日
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 件のコメント
Bruno Luong
2022 年 9 月 14 日
編集済み: Bruno Luong
2022 年 9 月 14 日
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.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!