save/export a collection of matrices from a loop without overwriting
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Star Strider
am 3 Sep. 2012
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?
Antworten (3)
Image Analyst
am 3 Sep. 2012
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 Kommentare
dirk-jAn
am 5 Sep. 2012
2 Kommentare
Image Analyst
am 5 Sep. 2012
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.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!