How to run a for-loop with different dimesions?
    3 views (last 30 days)
  
       Show older comments
    
    Wan-Yi Chiu
 on 22 Apr 2020
  
    
    
    
    
    Commented: Wan-Yi Chiu
 on 22 Apr 2020
            Let A be a 20 by 1 array. I can separately compute 
B_1=mean(A(1:6, 1:1)), 
B_2=mean(A(7:15, 1:1)), and 
B_3=mean(A(16:20, 1:1)). 
How can I program this job using the loop command with respect to different dimensions?
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 22 Apr 2020
        starts = [1 7 16];
stops  = [6, 15, 20];
n = length(starts);
B = zeros(1,n);
for K = 1 : n
    B(K) = mean(A(starts(K):stops(K), 1));
end
More Answers (0)
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!