How to efficiently find maximum of selected indices of an array in MATLAB?
    1 Ansicht (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    RZM
 am 11 Apr. 2018
  
    
    
    
    
    Beantwortet: Guillaume
      
      
 am 11 Apr. 2018
            Let us say we have the vectors: w, s_on, and s_off. s_on and s_off have the ascending indices of the onset and offset of an event. We need to find the maximum value in during each event. How can we do it without using loop. for example we may have the following values:
    s_on = [5 19 78 101];
    s_off = [10 28 97 152];
    w = rand(1,200);
The following code does not work:
    pv = max(w(s_on(1:end):s_off(1:end)))
0 Kommentare
Akzeptierte Antwort
  Guillaume
      
      
 am 11 Apr. 2018
        You don't have a choice, a loop is required for that. Either an explicit one:
pv = zeros(size(s_on)); 
for idx = 1:numel(s_on)
  pv(idx) = max(w(s_on(idx):s_off(idx)));
end
Or through an arrayfun:
pv = arrayfun(@(s, e) max(w(s:e)), s_on, s_off);
It is likely that the explicit loop will be faster.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Loops and Conditional Statements 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!
