how to batch save and load loop variable
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello, everyone
I want to access the result of each calculation in the loop calculation I would like to ask if MATLAB has any instructions for batch storage and batch input of variables? and I want to make each stored variable have a different name
ex code
for i = 1:3
a = rand(1,10)
end
a =
0.5817 0.5337 0.8386 0.3502 0.3441 0.9860 0.8664 0.5777 0.2776 0.2758
a =
0.2768 0.5556 0.1333 0.3556 0.8931 0.9569 0.0273 0.2038 0.2547 0.7161
a =
0.2658 0.2586 0.4210 0.5831 0.6447 0.0349 0.2437 0.3171 0.3266 0.4875
I want to convert each generated a into a mat file and be able to read these three sets of a to matlab
Because using normal access will be overwritten because the name is all a
댓글 수: 1
Stephen23
2022년 6월 19일
"Because using normal access will be overwritten because the name is all a"
Only if you write code that overwrites that variable.
"I want to make each stored variable have a different name"
That would be a very complex and inefficient approach, whereas you could just use simple and efficient indexing.
What is stopping you from using basic indexing?
답변 (2개)
KALYAN ACHARJYA
2022년 6월 19일
편집: KALYAN ACHARJYA
2022년 6월 19일
#Cell Array cell
a=cell(1,3);
for i = 1:3
a{i}=rand(1,10)
end
Once strore in the cell array, you can do/access data in easiest & efficient way.
댓글 수: 0
Voss
2022년 6월 19일
How about making a a matrix?
a = zeros(3,10);
for i = 1:3
a(i,:) = rand(1,10);
end
disp(a);
Or, if each iteration produces a vector of a different length, make a a cell array:
a = cell(3,1);
for i = 1:3
a{i} = rand(1,randi(10));
end
disp(a);
There is no need to make several variables; use one variable (matrix, cell array, etc.) and index into that.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!