Make a loop

User ForumMake a loop

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:
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);
fac2_mat = arraytomat(fact2Final[i,.,.])';

file_name = "fac2group" \$+ ntos(i) \$+ ".xlsx";
xlsWriteM(fac2_mat, file_name, "a1", 1, "");
endfor;
```

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, "");
endfor;
```

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:

```ntos(i);
```

with this:

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

for versions older than GAUSS 14.

• Aptech Systems, Inc. Worldwide Headquarters

Aptech Systems, Inc.
2350 East Germann Road, Suite #21
Chandler, AZ 85286

Phone: 360.886.7100
FAX: 360.886.8922

• Training & Events

Want more guidance while learning about the full functionality of GAUSS and its capabilities? Get in touch for in-person training or browse additional references below.

• Tutorials

Step-by-step, informative lessons for those who want to dive into GAUSS and achieve their goals, fast.

• Have a Specific Question?

Get a real answer from a real person

• Need Support?
• Support Plans

Premier Support and Platinum Premier Support are annually renewable membership programs that provide you with important benefits including technical support, product maintenance, and substantial cost-saving features for your GAUSS System or the GAUSS Engine.

• User Forums

Join our community to see why our users are considered some of the most active and helpful in the industry!