How to write for loop for an array calculation ?
    5 views (last 30 days)
  
       Show older comments
    
    Jeevan Kumar Bodaballa
      
 on 2 Apr 2021
  
    
    
    
    
    Edited: Sajid Afaque
      
 on 2 Apr 2021
            I am trying to use below code to calculate and write into new variable but it's taking last value and giving same whole vector. 
for k=1:3
    for j = 22:24 
        %three values expected and write into A
      A(k,:) = (f1_lh_nwaer(j,25)+f1_lh_nwaer(j+1,25))/2; 
    end
end
output: 
A = [0.5;0.5;0.5]
5 Comments
  Sajid Afaque
      
 on 2 Apr 2021
				
      Edited: Sajid Afaque
      
 on 2 Apr 2021
  
			Great that it worked. @Jeevan Kumar Bodaballa
if my comment helped you then please accept my answer, as my previous answer was in comment section
Accepted Answer
  Sajid Afaque
      
 on 2 Apr 2021
        if i understood correctly then,
it is because your inner loop runs over j several times for each k value
hence in A the k position which you have given is overwritter for each j.
count = 1;
for k=1:3
    for j = 22:24 
        %three values expected and write into A
      A(count,:) = (f1_lh_nwaer(j,25)+f1_lh_nwaer(j+1,25))/2; 
      count = count+1;
    end
end
if this doesnt solves your query then please do explain your problem in other way as is it difficult to understand your current description of problem
0 Comments
More Answers (0)
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

