For Programmers: Free Programming Magazines  


Home > Archive > Matlab > June 2007 > mx Function, I don't understand an example









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author mx Function, I don't understand an example
Laura

2007-06-29, 8:13 am

Hi!! I'm using mx functions and I was looking for an example of how
to convert an i and j indexes in a single subscript. Someone talked
me about the mxCalcSingleSubscript and I've found the example but I
don't understand the sentences:

tempM=mxGetPr(Matrix);
// Check if the size of the array is the same than the size of sbus
for(x=0;x<nsubs;x++)
{
// Matlab index array starts in '1' but C index array starts in '0'
subs[x]=(mwIndex)temp[x]-1;
if((tempM[x]>((mxGetDimensions(Matrix))[x]))
{
mxFree(subs);
mexErrMsgTxt("You indexed above the size of the array.");
}
}

Someone can help me? Thanks
Praetorian

2007-06-29, 7:14 pm

On Jun 29, 4:23 am, Laura <molak...@hotmail.com> wrote:
> Hi!! I'm using mx functions and I was looking for an example of how
> to convert an i and j indexes in a single subscript. Someone talked
> me about the mxCalcSingleSubscript and I've found the example but I
> don't understand the sentences:
>
> tempM=mxGetPr(Matrix);
> // Check if the size of the array is the same than the size of sbus
> for(x=0;x<nsubs;x++)
> {
> // Matlab index array starts in '1' but C index array starts in '0'
> subs[x]=(mwIndex)temp[x]-1;
> if((tempM[x]>((mxGetDimensions(Matrix))[x]))
> {
> mxFree(subs);
> mexErrMsgTxt("You indexed above the size of the array.");
>
> }
> }
>
> Someone can help me? Thanks


Laura,
The example you've posted is incorrect (I'm not saying the TMW version
is incorrect but I think you've changed portions while typing it
here); I managed to find the original example myself. The variable
'temp', which you call both 'temp' and 'tempM' in various places in
your example, is not being assigned a pointer to your input matrix; it
is being assigned a pointer to the vector specifying the index that
you want to access (the [i,j] element that you're trying to convert
into a single subscript). The section of code you've posted checks
whether the matrix element you're trying to access is actually within
the matrix bounds. For example, if you have a 4x4 matrix then the last
element, with [i,j] zero-based indexing, is [3,3]. If you ask for an
element exceeding either of those numbers then the code will throw an
error. The reason for subtracting 1 from 'temp' is because the input
to this mex-function is 1-based numbering while the C code in it uses
0-based numbering.

HTH,
Ashish.

Peter Boettcher

2007-06-29, 7:14 pm

Laura <molakey2@hotmail.com> writes:

> Hi!! I'm using mx functions and I was looking for an example of how
> to convert an i and j indexes in a single subscript. Someone talked
> me about the mxCalcSingleSubscript and I've found the example but I
> don't understand the sentences:
>
> tempM=mxGetPr(Matrix);
> // Check if the size of the array is the same than the size of sbus
> for(x=0;x<nsubs;x++)
> {
> // Matlab index array starts in '1' but C index array starts in '0'
> subs[x]=(mwIndex)temp[x]-1;
> if((tempM[x]>((mxGetDimensions(Matrix))[x]))
> {
> mxFree(subs);
> mexErrMsgTxt("You indexed above the size of the array.");
> }
> }
>
> Someone can help me? Thanks


You've gone too far into detail. Back up a little and give some
context. I guess you want to know how to process a 2D matrix from a
MEX file?

Easiest way:

double *data;
int M, N;
// Matrix is an mxArray *

data = mxGetPr(Matrix);
M = mxGetM(Matrix);
N = mxGetN(Matrix);

for(j=0; j<N; j++)
for(i=0; i<M; i++) // Column loop on the inside for efficiency
// do something with data[j*M + i]

Does that help?

-Peter
Laura

2007-06-29, 7:14 pm

Thank you very much!! That is much easier.Thank you
Laura

2007-06-30, 8:12 am

Hi!!I'm using your idea of making:
double *data;
int M, N;
// Matrix is an mxArray *

data = mxGetPr(Matrix);
M = mxGetM(Matrix);
N = mxGetN(Matrix);

for(j=0; j<N; j++)
for(i=0; i<M; i++) // Column loop on the inside for efficiency
// do something with data[j*M + i]

But I have a question, I know in C the matrix are a 0 based and in
Matlab a 1 based, but if I use mxArray in which number starts?in 0 or
in 1?
I've found that in the Matlab Documentation:
For example, to express the starting element of a two-dimensional
mxArray in subs, set subs[0] to 0 and subs[1] to 0.
So I think is a 0 based matrix,isnt'is?
Thank you.
Laura

2007-06-30, 8:12 am

Hi!!I'm using your idea of making:
double *data;
int M, N;
// Matrix is an mxArray *

data = mxGetPr(Matrix);
M = mxGetM(Matrix);
N = mxGetN(Matrix);

for(j=0; j<N; j++)
for(i=0; i<M; i++) // Column loop on the inside for efficiency
// do something with data[j*M + i]

But I have a question, I know in C the matrix are a 0 based and in
Matlab a 1 based, but if I use mxArray in which number starts?in 0 or
in 1?
I've found that in the Matlab Documentation for the
mxCalcSingleSubscript function :
For example, to express the starting element of a two-dimensional
mxArray in subs, set subs[0] to 0 and subs[1] to 0.
So I think is a 0 based matrix,isnt'is?
Thank you.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com