How to store matrices in a cell from a loop?
이전 댓글 표시
I am running an external software (GAMS) to do some optimization where I call GAMS in a loop and get back 16 variables (matrices) that I want to store in a cell in every iteration from the loop. I have trouble doing so without delcaring the variables in the workspace first. See bellow what I did. Thanks.
for k = 1:55
% Some calculations in every iteration (k)
% Call GAMS model in every iteration (k) and get 16 variables
[Var1 Var2 Var3 ...] = gams('My_model.gms')
% This is what I do now but want to modify
Var1(:,k) = Var1.val(:,2);
Var2(:,k) = Var2.val(:,2);
Var3(:,k) = Var3.val(:,2);
end
Instead of declaring every variable in the workspace, which are matrices (24x55), I want to store the variables in a cell array: My_cell = {1,16}, where each cell is the matrix (24x55) that results from the iterations and I need to add a header to each cell to identify the variables.
The difficulty I have is that every matrix is written in sequence (k) e.g, k=1 (24x1), k=2 (24x2) ... k=55 (24x55) and at the same time I need to fill the cells of My_cell = {1,16} in the loop. I have tried with a nested loop without success.
댓글 수: 2
Sambit Supriya Dash
2021년 7월 15일
Could you put an example as the code, and should exactly be the output, so that it would be more clear to address.
Pedro Leal
2021년 7월 15일
채택된 답변
추가 답변 (1개)
I am taking random data to show how you can store the output in a cell array using loop.
for index=1:16
output{index}=randi(100,25,55);
end
output
You can do even without using loop too.
arrayfun(@(x) randi(100,25,55), 1:16, 'uni', 0)
카테고리
도움말 센터 및 File Exchange에서 Descriptive Statistics and Insights에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!