Dear Stata community,

I want to save matrices from each iteration of an r-class program which I am looping to do Monte-Carlo simulations. I have already written a program in which I save the matrices in a cumulative matrix, which works fine if you are not building very large matrices. However, now I am doing 10.000 loops and my program has become very slow, too slow to be useful actually. To solve this issue I read about the -post- command and tried to implement it, however I cannot figure out how to store matrices with -post- is this even possible? It works fine for scalars and the program is very fast that way, but I need to store matrices as well.

Please find a piece of my code below:

Code:
forvalues i=1/`nreps' {
            preserve
                bsample `bootstrap_period'
                display _newline(2) `i'
                capture noisily ovport `developed_equities' `emerging_equities' `developed_gov_bond' `emerging_gov_bond' `developed_corp_bond' `real_estate' , casewise nport(`portfolios') noshort rfrate(`rf1')
                
                matrix this_run_weights = r(weights)                                    // Optimal weight for each asset class under tangency with the efficien frontier
                matrix colnames this_run_weights = run_`i'
                matrix cumu_weights_raw = nullmat(cumu_weights_raw),this_run_weights