Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: help regarding 2D matrix using c mex file
Date: Thu, 20 Aug 2009 08:41:03 +0000 (UTC)
Organization: Indian Institute of Technology
Lines: 47
Message-ID: <h6j26v$n34$1@fred.mathworks.com>
References: <h6b2k6$oh2$1@fred.mathworks.com> <h6b3nq$6ft$1@fred.mathworks.com> <h6dsve$37p$1@fred.mathworks.com> <h6et12$284$1@fred.mathworks.com> <h6fhbo$l34$1@fred.mathworks.com> <h6gav1$hjm$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1250757663 23652 172.30.248.38 (20 Aug 2009 08:41:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 20 Aug 2009 08:41:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 223861
Xref: news.mathworks.com comp.soft-sys.matlab:564688


Dear James,
Thank you very much for your detailed steps. 
I have made the changes and here is the output I obtained.
I actually wanted my output (for some later computations in C) the *output to be format output[][]. How can I obtain that?


with regards,
ramana
*********
#include "mex.h"
#include <math.h>

#define OUT        plhs[0]
void py_dist(double *a, mwSize xDim, mwSize yDim, double *output) {
    int x, y;
    for(x = 0; x<=xDim; ++x)
        for (y = 0; y<=yDim; ++y)
        { double t = *a++;
          *output++ =  t*t;
        }
}

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
    double *output, *a;
    mwSize xDim, yDim;
    
    xDim = mxGetM(prhs[0]);
    yDim = mxGetN(prhs[0]);
    /*do the matlab stuff*/
    mexPrintf("x Dimensions = %u.\n", xDim);
    mexPrintf("y Dimensions = %u.\n", yDim);
    OUT = mxCreateDoubleMatrix(xDim, yDim, mxREAL); // mxArray is transpose of c matrix
    output = mxGetPr(OUT);
    a = mxGetPr(prhs[0]);
    py_dist(a, xDim, yDim, output);
}
********
>> mex Demo3.c
>> a = [1 2 3; 3 4 5];
>> Demo3(a)
x Dimensions = 2.
y Dimensions = 3.

ans =

     1     4     9
     9    16    25