Store for loop data in cell array

조회 수: 4 (최근 30일)
Matlab_G
Matlab_G 2022년 11월 21일
댓글: Matlab_G 2022년 11월 21일
Hello,
I would like to store the montly_average data from the for loop in a 6x1 cell array where each element is a 12x1 cell array of the months.
for i=2000:2005
for j=1:12
monthly_average=mean(data_file(3,data(1,:)==i & data_file(2,:)==j))
end
end
Thanks in advance

채택된 답변

Walter Roberson
Walter Roberson 2022년 11월 21일
years = 2000:2005;
months = 1 : 12;
num_years = length(years);
num_months = length(months);
monthly_average_cell = cell(num_years, 1);
for year_idx = 1 : num_years
i = years(year_idx);
month_cell = cell(1, num_months);
for month_idx = 1 : num_months
j = months(month_idx);
month_cell{month_idx} = mean(data_file(3,data(1,:)==i & data_file(2,:)==j));
end
monthly_average_cell{year_idx} = month_cell;
end
  댓글 수: 1
Matlab_G
Matlab_G 2022년 11월 21일
Thank you so much! it works perfectly

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by