how to batch save and load loop variable
5 次查看(过去 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 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!