How to store each iteration data, which is having multiple loops
1 次查看(过去 30 天)
显示 更早的评论
Hai,
I have two for loop in a program, one for iteration and other for matrix formation. I need to store all iteration data together.
sample program:
for i = 1:3
for j = 1:10
x1 = 3*rand(1,1);
x2 = 4*rand(1,1);
x3 = 5*rand(1,1);
x = [x1, x2, x3];
s(j,:) = x;
end
s
end
I need to store s1, s2, s3 together to another variable p.
Thanking You...
0 个评论
采纳的回答
Jakob B. Nielsen
2020-1-21
You can add a third dimension to your matrix easily enough, your result p will now be a 3D matrix. (You can even add a 4th dimension and beyond, but it becomes harder and harder to "imagine" in your head ;) )
for i = 1:3
for j = 1:10
x1 = 3*rand(1,1);
x2 = 4*rand(1,1);
x3 = 5*rand(1,1);
x = [x1, x2, x3];
s(j,:) = x;
end
p(:,:,i)=s;
end
Or, another approach would be to use a structure, depending on your preference really.
for i = 1:3
for j = 1:10
x1 = 3*rand(1,1);
x2 = 4*rand(1,1);
x3 = 5*rand(1,1);
x = [x1, x2, x3];
s(j,:) = x;
end
p(i).sdata=s;
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!