save/export a collection of matrices from a loop without overwriting

2 ビュー (過去 30 日間)
dirk-jAn
dirk-jAn 2012 年 9 月 3 日
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
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?
dirk-jAn
dirk-jAn 2012 年 9 月 5 日
The error occurs when I want to use subscripts on the created variables (for example (i) if I change the counter name as well to i)

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2012 年 9 月 3 日

Image Analyst
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.

dirk-jAn
dirk-jAn 2012 年 9 月 5 日
Ok sorry, it might have been confusing. I did not get any error using this code, just when I try to save or export it, it overwrites the earlier output. I imagined that I should change my counter to gather this data in a 2D array, but doing this (using ´i´), I every time get this error message when using the same subscript (e.g. rd(i)=rand(3) ) in one of the equations. Can anyone tell me how exactly I need to save all the BB matrices in a 2D array in this case?
  2 件のコメント
Image Analyst
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.
dirk-jAn
dirk-jAn 2012 年 9 月 5 日
It´s a random 3x3 matrix. For a project, I need take Q matrices (From QR decomposition) of random matrices to multiply with the given matrix B and finally store all the outcomes that satisfy the given conditions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by