Make a loop

User ForumMake a loop
marvin2jh asked 2 years ago

I would like to use a loop to simplify the belows. Is there anyone to let me know how?

Dividing 6*m*n array into 6 m*n matrices. Then after transposing 6 matrices, save them as xlsx file.

I'd appreciate it.
fac2group1 = arraytomat(fact2Final[1,.,.]);
fac2group2 = arraytomat(fact2Final[2,.,.]);
fac2group3 = arraytomat(fact2Final[3,.,.]);
fac2group4 = arraytomat(fact2Final[4,.,.]);
fac2group5 = arraytomat(fact2Final[5,.,.]);
fac2group6 = arraytomat(fact2Final[6,.,.]);

fact2group1= fac2group1'
fact2group2= fac2group2'
fact2group3= fac2group3'
fact2group4= fac2group4'
fact2group5= fac2group5'
fact2group6= fac2group6'

xlswritem(fact2group1, "fact2group1.xlsx", "a1", 1, "");
xlswritem(fact2group2, "fact2group2.xlsx", "a1", 1, "");
xlswritem(fact2group3, "fact2group3.xlsx", "a1", 1, "");
xlswritem(fact2group4, "fact2group4.xlsx", "a1", 1, "");
xlswritem(fact2group5, "fact2group5.xlsx", "a1", 1, "");
xlswritem(fact2group6, "fact2group6.xlsx", "a1", 1, "");

Question Tags:
3 Answers
aptech Staff answered 2 years ago

Here is some code that will do what you ask and will continue to work if you increase the first dimension of the array to more than six.

//get dimensions of 'fact2Final'
orders = getOrders(fact2Final);

//First element will contain number
//of matrices in the 3D array
num_mats = orders[1];

for i(1, num_mats, 1);
   //Add transpose to assignment step
   fac2_mat = arraytomat(fact2Final[i,.,.])';
   file_name = "fac2group" $+ ntos(i) $+ ".xlsx";
   xlsWriteM(fac2_mat, file_name, "a1", 1, "");

Or if you wanted to condense it a bit, you could make it short like this:

orders = getOrders(fact2Final);

for i(1, orders[1], 1);
   xlsWriteM(arraytomat(fact2Final[i,.,.])', "fac2group" $+ ntos(i) $+ ".xlsx", "a1", 1, "");
marvin2jh answered 2 years ago

Hello. Thanks a lot. But an error came out, saying ntos is undefined symbol.

aptech Staff answered 2 years ago

Oh, yes. ntos is a new convenience function in GAUSS 14. You can replace:


with this:

cvtos(ftocv(i, 0, 0));

for versions older than GAUSS 14.