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

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 comentarios

what is this?
for rd=1:50;
rd=rand(3);
%rd is used as a counter and as a random matrix!
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?
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)

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 3 de Sept. de 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.
dirk-jAn
dirk-jAn el 5 de Sept. de 2012
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 comentarios

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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Sept. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by