Counting average on huge matrix with conditional
Mostrar comentarios más antiguos
The following code runs forever on 10^5 square matrix A. It runs a few seconds on 10^3 square matrix. Is there any way to speed it up?
function [meanNV]=calcMeanNV_k(A,V,d)
%A: input matrix (n x n)
%V: eigenvectors of k-th largest eigenvalues (n x 1)
%d: degrees of each node (n x 1)
%meanNV: mean of neighbors vectors (n x 1)
m=size(A,2);
meanNV=zeros(113336);
for i=1:m
sumNode = 0;
for j=1:m
if A(i,j)==1
sumNode=sumNode+V(j);
end
end
[meanNV(i)]=sumNode/d(i);
end
2 comentarios
Brendan Hamm
el 28 de Jun. de 2016
What exactly is this code supposed to be doing?
It seems as though you are finding the last 1 in each row of A. You are taking the corresponding value of V (at the location from A). You are then dividing this by the corresponding value in d.
- The size of a 10^5x10^5 double matrix would be 74.5GB. Do you have enough memory to support this?
- I am doubting that the code does what you are thinking it does, but I can't tell either because I don't know what A represents. It does exactly what I describe above.
- Does meanNV need to be a 113336x113336 matrix? You mention A has similar dimensions to this, but you are only storing m elements in meanNV (one for each row of A), so I think you mean to make this:
meanNV = zeros(m,1); % Save lots of space here.
- You can avoid for loops to improve speed. Use logical criteria. The variable below contains a one in every location where A(i,j) == 1 with no need for a loop.
idx = A == 1; % Or is A logical already?
- Now we can loop through the rows and find the last element with a 1:
for k = 1:m
col = find(idx(k,:));
col = col(end);
meanNV(k) = V(col)/d(k);
end
Amin Rois Sinung Nugroho
el 30 de Jun. de 2016
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!