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!