Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Generating a huge array.
Date: Sat, 10 Apr 2010 07:34:07 +0000 (UTC)
Organization: Boeing Co
Lines: 34
Message-ID: <hpp9lf$g1f$1@fred.mathworks.com>
References: <hpp13j$o1i$1@fred.mathworks.com> <hpp2rr$h1q$1@fred.mathworks.com> <hpp3ih$pp4$1@fred.mathworks.com> <hpp5ug$r20$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1270884847 16431 172.30.248.35 (10 Apr 2010 07:34:07 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 10 Apr 2010 07:34:07 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 756104
Xref: news.mathworks.com comp.soft-sys.matlab:625292

After looking at Roger's post I realized I had a typo in my code. The for loop tests were ending prematurely and need <= tests instead of < tests.

#include "mex.h"
#define N 100
#define NDOUBLE 100.0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    char i, j, k;
    char *sp, *sp0;
    double *pr;
    mwSize x, n;
    
    sp = sp0 = mxMalloc( N*N*N*N*sizeof(*sp) );
    for( i=0; i<=N; i++ ) {
        for( j=0; j<=N; j++ ) {
            for( k=0; k<=N; k++ ) {
                if( i+j+k <= N ) {
                    *sp++ = i;
                    *sp++ = j;
                    *sp++ = k;
                    *sp++ = N - i - j - k;
                }
            }
        }
    }
    n = (sp - sp0) / 4;
    sp = sp0;
    plhs[0] = mxCreateDoubleMatrix(4, n, mxREAL);
    pr = mxGetPr(plhs[0]);
    for( x=0; x<4*n; x++ ) {
        *pr++ = *sp++ / NDOUBLE;
    }
    mxFree(sp0);
}