Can this for-loop code get faster in some way?
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
I got a big result table named resTbl.
There I need for each row to grab a timestamp (posix time) and construct a period interval vector (I use a constant, ts_length, to construct this).
Then I need to find all those in period_interval that are represented in the variable data (column 2), and take the sum of those in data (column 7).
The below code works as I want it to:
for i = 1 : size(resTbl ,1)
    period_interval =  resTbl(i,2) : 60000 : resTbl(i,2) + ts_length;
    [hd, he] = ismember(period_interval,data(:,2));
    resTbl(i,10) = sum(data(he(hd),7));                                   
end
The problem is that it is slow since resTbl has many rows. Does anyone have a suggestion how to make it faster?
Respuestas (1)
  darova
      
      
 el 5 de Oct. de 2019
        
      Editada: darova
      
      
 el 5 de Oct. de 2019
  
      Maybe ismember function can be replaced:
dt = 60000;
period_interval =  0 : dt : ts_length;
n = length(period_interval);
for i = 1 : size(resTbl ,1)
    cond = ~mod( data(:,2)-resTbl(i,2),dt );            % multiple by 60 000
    mult = (data(:,2)-resTbl(i,2))/dt;
    ind = (0 <= mult & mult <= n) & cond;               % (0 <= multiplier <= n) and multiple by 60 000
%     [hd, he] = ismember(period_interval,data(:,2));
    resTbl(i,10) = sum(data(ind,7));                                   
end
4 comentarios
Ver también
Categorías
				Más información sobre Matrix Indexing en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


