Create new array for each iteration ?

11 次查看(过去 30 天)
Mini Me
Mini Me 2017-4-17
编辑: Mini Me 2017-4-18
Ok so I have a nested for loop that goes like this:
for i=1:size(L,2) ----L is vector(1:1:7)
filename = ['file_',num2str(L(i))]
filename1 = 'Total result';
Array1 =[]
for j=1:size(M,1)-----M is a vector (3:1:9)
C= do some calculation
Array1 = [Array1;C]
csvwrite(filename,Array1)
end
end
I want to be able to take the values of the array1 for the first iteration and second iteration and add them to get sum of total value. Take the total values and feed it in a new array. I can't seem to get my head around that. Also I want to be able to write the values of Array 1 in each iteration of i and the total values into one csv file. Right now my first and second iteration write to different file purposely. Can you help me and point me in the right direction,please? The most important thing is to actually get the array1 at first iteration then the second array1 at 2nd iteration and add them up once the loop is done. I want the results to be written in the file like this after all the calculation has been done.
Outer loop 1 - Outer loop 2 - Total
Inner loop 1 - Inner loop 2 - inner Loop1 + inner loop 2
Inner loop 1 - inner loop 2
Inner loop 1 - inner loop 2
Inner loop 1 - inner loop 2
  3 个评论
Stephen23
Stephen23 2017-4-18
@Mini Me: it is not clear what you are trying to achieve. Whatever you do, do NOT try to access lots of arrays in a loop:
Mini Me
Mini Me 2017-4-18
编辑:Mini Me 2017-4-18
@jan Simon 1st and second iteration of the outer loop. I do not want to overwrite the file at all. At each iteration of the outer loop, it should append everything generated in the inner loop by rows. Then when it goes to the next iteration of the outer loop it should append the next set of results if the inner loop in the next column. For example.
Outer loop 1 Outer loop 2 Total
Inner loop 1 Inner loop 2 inner Loop1 + inner loop 2
Inner loop 1 inner loop 2
Inner loop 1 inner loop 2
Inner loop 1 inner loop 2

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by