save/export a collection of matrices from a loop without overwriting
2 次查看(过去 30 天)
显示 更早的评论
I created a loop to collect a number of matrices that fulfill some restrictions put on the modification of a random matrix. Now I want to store these matrices BB, saving in .mat as well as saving in Excel (preferred), but the last value overwrites the previous value:
B=[...
7.797562562, -0.832787948, -1.725054903;...
2.11093262, 3.138528042, -0.326926679;...
2.128339023, -0.061787665, 6.644309749];
for rd = 1 : 50;
rd=rand(3);
[Q,R]=qr(rd);
Q(1,1)=Q(1,1)*-1;
Q(2,1)=Q(2,1)*-1;
Q(3,1)=Q(3,1)*-1;
D=Q';
C=B*D;
if C(1,1)>0 && C(2,2)>0 && C(3,3)>0 && C(3,1)<0 && C(3,2)>0,
BB=C;
end
save test.mat BB -append
xlswrite('file.xls', BB, B2);
end
I also tried to use subscripts for every loop, but then I get this notification from MATLAB:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Does anybody have an answer? It's also fine to store every value of the BB matrices separate (like BB(2,1) etc.).
3 个评论
Star Strider
2012-9-3
I ran your code several times (except for the save and xlswrite statements) and it ran without an error.
What line is giving you the error? What are the contents of the matrices that give you the error?
回答(3 个)
Image Analyst
2012-9-3
Do you want the data from all the iterations saved in separate files, or all in the same file? If you want separate files, make a new filename on each iteration according to the FAQ Walter referred you to. Otherwise, collect all the data in a 2D array or cell array and write it out once, to one file, after your loop exits.
By the way, you should change your loop counter from rd to k or loopIndex or something. It is separate from the rd inside the loop and changing rd inside the loop to a random set of three numbers will not change the loop iterations - there will still be 50 of them. But it's better to avoid confusion and not use the same names.
0 个评论
dirk-jAn
2012-9-5
2 个评论
Image Analyst
2012-9-5
We have no idea why you're getting 3 random numbers in the first place, and then trying to stuff them all into a single element of an array.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!