# Storing values from a nested FOR loop (can't get how to save the output into a matrix)

1 view (last 30 days)
ZenithWoman on 7 Dec 2016
Commented: ZenithWoman on 7 Dec 2016
• I am new to coding so pardon me if this questions seems to easy to be asking but I have already spent hours searching online*
I have an 80 column matrix. I want to write a loop that takes the average of 1st item and the 11th item and in that order till the average of 70th and 80th item.I should have 40 columns in the end. I was able to write the loop to do that but because I can't get the output of each iteration saved I only get the output of the last iteration. Can someone please help my with what I need to add to the code below and possibly an explanation how the whole thing should be just for future reference.
Average_smthdata = zeros(length(smthdata),40); %preallocation
for i = 1:1:70
Average_smthdata = mean([smthdata(1:end,i),smthdata(1:end,i+10)],2);
end;
Thanks! Felix

Jiro Doke on 7 Dec 2016
Your for loop goes from 1 to 70, so it looks like you're trying to generate 70 columns of data, not 40.
Aside from that, you would change the left hand side of your code inside the for loop to
Average_smthdata(:,i) =
##### 2 CommentsShow NoneHide None
ZenithWoman on 7 Dec 2016
Thanks! Jiro. It worked and also you're right. My loop was running until 70. I adjusted it to the below code. It gave the right output but the problem now is I end up with 70 columns since my i is up to 70. What can I do to keep it at 40? I have zeroes in columns 11-20,31-40,51-60. So I entered another loop inside. This kept it to 40 columns but returned only the value of the last value of i
Average_smthdata = zeros(length(smthdata),40);
for i = [1:10,21:30,41:50,61:70]
for s = 1:40
Average_smthdata(:,s) = mean([smthdata(1:end,i),smthdata(1:end,i+10)],2);
end;
end;
What am I missing?
ZenithWoman on 7 Dec 2016
Average_smthdata = zeros(length(smthdata),40);
for i = 1:4
for s = 1:10
k = (i-1)*10 + s;
if i ==1
Average_smthdata(:,k) = mean([smthdata(1:end,k),smthdata(1:end,k+10)],2);
elseif i==2
Average_smthdata(:,k) = mean([smthdata(1:end,k+10),smthdata(1:end,k+20)],2);
elseif i==3
Average_smthdata(:,k) = mean([smthdata(1:end,k+20),smthdata(1:end,k+30)],2);
else
Average_smthdata(:,k) = mean([smthdata(1:end,k+30),smthdata(1:end,k+40)],2);
end
end;
end;
This finally works but is there a better way around it? I will love any inputs.
In summary, the code should take the 1st and 11th item, average it and return the output in the first column. then the 2nd and 12th item until the 70th and 80th item average and by the end, I should have half the columns of the original data. So, defining the window in i gave me zeros in the outputs like I mentioned earlier.
I work with big data and will love to learn how to write loops etc. Any tutorial will be appreciated

### 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!