Hello to you all,
i've been struggling for a while with my first mex file. Basically it is a simlpe program with 2 for loops, one along i and one along j. If some condition is satisfied i need to remember the specific i,j pair. So in the end i want to have a [? x 2] matrix. The complete code is written bellow. I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
Any comments on how to make this programm work ??
thank you
blaz
#include <matrix.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//declare variables
mxArray *ts, *g;
double *x, *y;
mwSize l1, l2;
mwIndex i, j, jj, k, *indi, *indj, numalloc;
void *tmpi, *tmpj;
const mwSize *dim;
//associate inputs
ts = mxDuplicateArray(prhs[0]);
//figure out dimensions
l1 = mxGetM(prhs[0]); //number of rows
l2 = mxGetN(prhs[0]); //number of columns
//checking whether the input really is a scalar time series
if ((l1 != 1) & (l2 != 1))
mexErrMsgTxt("Input must be a scalar time series");
//set the length of time series to l2
if (l1 < l2){
l2 = l2;
}
else{
l2 = l1;
}
//associate pointers
x = mxGetPr(ts);
//do something
//mexPrintf("The length of time series is %d\n",l2);
k = 0;
numalloc = 1; //allocated memory
indi = mxCalloc(numalloc,sizeof(mwIndex)); //array for indices i
indj = mxCalloc(numalloc,sizeof(mwIndex));
for(i=0;i<l2-1;i++)
{
indi[k] = i+1;
indj[k] = i+2;
k++;
if(k == numalloc){
numalloc *=2;
indi = mxRealloc(indi,numalloc*sizeof(mwIndex));
indj = mxRealloc(indj,numalloc*sizeof(mwIndex));
//indi = (mwIndex*)tmpi;
//indj = (mwIndex*)tmpj;
mxFree(tmpi);
mxFree(tmpj);
}
if(i<l2-2)
{
for(j=i+2;j<l2;j++)
{
jj = i+1;
while ((jj<j) & (x[jj] < (x[j]+(x[i]-x[j])*(j-jj)/(j-i))))
{
jj++;
}
if (jj == j)
{
indi[k] = i+1;
indj[k] = j+1;
k++;
if(k == numalloc){
numalloc *=2;
indi = mxRealloc(indi,numalloc*sizeof(mwIndex));
indj = mxRealloc(indj,numalloc*sizeof(mwIndex));
//mxSetPr(indi,tmpi);
//mxSetPr(indj,tmpj);
mxFree(tmpi);
mxFree(tmpj);
}
}
}
}
}
//associate outputs
g = plhs[0] = mxCreateDoubleMatrix(k,2,mxREAL);
y = mxGetPr(g);
for(i=0;i<k;i++)
{
y[i] = indi[i];
y[k+i] = indj[i];
}
return;
}
|
|
0
|
|
|
|
Reply
|
blaz.krese (9)
|
2/4/2010 8:34:02 AM |
|
Dear blaž!
> I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
Do not reallocate the memory inside a loop. It is much more efficient to allocate the maximal needed memory at the beginning and reallocate at the end once.
Good luck, Jan
|
|
0
|
|
|
|
Reply
|
matlab.THIS_YEAR1 (1196)
|
2/4/2010 9:42:02 AM
|
|
Hi Jan,
thanks for the answer. Before i forgot to mention, that my matlab gives me ms visual c++ runtime error when i call this mex function, or it says to save the workspace and exit. So yes i will put the allocation of the memory before the loop but i still need to know how to do this since my way is obviously wrong. So how do i alocate the memory and set all the pointers right according to my example
thanks
"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hke4pa$ecf$1@fred.mathworks.com>...
> Dear blaž!
>
> > I am positive that the problem is with mxRealloc function, but i just can't make it work properly.
>
> Do not reallocate the memory inside a loop. It is much more efficient to allocate the maximal needed memory at the beginning and reallocate at the end once.
>
> Good luck, Jan
|
|
0
|
|
|
|
Reply
|
blaz.krese (9)
|
2/4/2010 9:53:05 AM
|
|
Dear blaž!
Allocate the memory at once:
numalloc = l2; //allocated memory
indi = mxCalloc(numalloc,sizeof(mwIndex));
indj = mxCalloc(numalloc,sizeof(mwIndex));
Do not free undefined pointers!
> mxFree(tmpi);
> mxFree(tmpj);
Good luck, Jan
|
|
0
|
|
|
|
Reply
|
matlab.THIS_YEAR1 (1196)
|
2/4/2010 10:24:04 AM
|
|
On 4 Feb, 09:34, "blaž " <blaz.kr...@siol.net> wrote:
> Hello to you all,
>
> i've been struggling for a while with my first mex file. Basically it is =
a simlpe program with 2 for loops, one along i and one along j. If some con=
dition is satisfied i need to remember the specific i,j pair. So in the end=
i want to have a [? x 2] matrix. The complete code is written bellow. I am=
positive that the problem is with mxRealloc function, but i just can't mak=
e it work properly.
Is this C or C++? If C++, use the std::stack<> container to store
intermediate results, and then allocate the necessary space for
the return variable at the very end, when you know how many
elements will be returned.
Apart from that, *never* base variable names on the single letters
'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
alone or in conjunction with only numbers. At some point you will
inevitably confuse them with the digits 1 or 0. *Only* use those
letters in textual labels, where it is obvious that they are letters
and not numbers.
Depending on the font settings in your code editor, you might not
be able to see the difference between the letters and numbers,
making for bugs that are extremely hard to track down, unless
it is obvious from the context what they are.
Rune
|
|
0
|
|
|
|
Reply
|
allnor (8474)
|
2/4/2010 12:01:11 PM
|
|
On Feb 4, 5:01=A0am, Rune Allnor <all...@tele.ntnu.no> wrote:
> On 4 Feb, 09:34, "blaž " <blaz.kr...@siol.net> wrote:
>
> > Hello to you all,
>
> > i've been struggling for a while with my first mex file. Basically it i=
s a simlpe program with 2 for loops, one along i and one along j. If some c=
ondition is satisfied i need to remember the specific i,j pair. So in the e=
nd i want to have a [? x 2] matrix. The complete code is written bellow. I =
am positive that the problem is with mxRealloc function, but i just can't m=
ake it work properly.
>
> Is this C or C++? If C++, use the std::stack<> container to store
> intermediate results, and then allocate the necessary space for
> the return variable at the very end, when you know how many
> elements will be returned.
>
> Apart from that, *never* base variable names on the single letters
> 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> alone or in conjunction with only numbers. At some point you will
> inevitably confuse them with the digits 1 or 0. *Only* use those
> letters in textual labels, where it is obvious that they are letters
> and not =A0numbers.
>
> Depending on the font settings in your code editor, you might not
> be able to see the difference between the letters and numbers,
> making for bugs that are extremely hard to track down, unless
> it is obvious from the context what they are.
>
> Rune
Why std::stack? IMHO std::vector or std::deque would be a much better
choice in his case since they'll allow him to retrieve the matches in
the order they were found.
- Ashish
|
|
0
|
|
|
|
Reply
|
ashish.sadanandan (750)
|
2/4/2010 2:22:23 PM
|
|
On 4 Feb, 15:22, Praetorian <ashish.sadanan...@gmail.com> wrote:
> On Feb 4, 5:01=A0am, Rune Allnor <all...@tele.ntnu.no> wrote:
>
>
>
>
>
> > On 4 Feb, 09:34, "blaž " <blaz.kr...@siol.net> wrote:
>
> > > Hello to you all,
>
> > > i've been struggling for a while with my first mex file. Basically it=
is a simlpe program with 2 for loops, one along i and one along j. If some=
condition is satisfied i need to remember the specific i,j pair. So in the=
end i want to have a [? x 2] matrix. The complete code is written bellow. =
I am positive that the problem is with mxRealloc function, but i just can't=
make it work properly.
>
> > Is this C or C++? If C++, use the std::stack<> container to store
> > intermediate results, and then allocate the necessary space for
> > the return variable at the very end, when you know how many
> > elements will be returned.
>
> > Apart from that, *never* base variable names on the single letters
> > 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> > alone or in conjunction with only numbers. At some point you will
> > inevitably confuse them with the digits 1 or 0. *Only* use those
> > letters in textual labels, where it is obvious that they are letters
> > and not =A0numbers.
>
> > Depending on the font settings in your code editor, you might not
> > be able to see the difference between the letters and numbers,
> > making for bugs that are extremely hard to track down, unless
> > it is obvious from the context what they are.
>
> > Rune
>
> Why std::stack?
Because of the semantics. The OP clearly does not know a lot
of C(++), so any talk about vectors would likely confuse him
about pre-allocating space etc, the same problems he tries
to solve with the code he posted.
> IMHO std::vector or std::deque would be a much better
> choice in his case since they'll allow him to retrieve the matches in
> the order they were found.
The concept - and semantics - of a stack is that one does not
care about memory allocations. They are taken care of internally.
Once you know the number of elements present, you can allocate
the necessary space in the vector, and fill in the elements
in the order they are popped from the stack, whihf is the reverse
order of the one theyr were found. Start filling in the elements
at the end of the vector, and work your way towards the start.
The end result is a list of hits, in the order they were found.
Rune
|
|
0
|
|
|
|
Reply
|
allnor (8474)
|
2/4/2010 2:30:19 PM
|
|
Dear blaž!
> Apart from that, *never* base variable names on the single letters
> 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> alone or in conjunction with only numbers.
Although this does not matter your question and sounds trivial - take Rune's advice seriously!
Jan
|
|
0
|
|
|
|
Reply
|
matlab.THIS_YEAR1 (1196)
|
2/4/2010 2:38:04 PM
|
|
On Feb 4, 7:30=A0am, Rune Allnor <all...@tele.ntnu.no> wrote:
> On 4 Feb, 15:22, Praetorian <ashish.sadanan...@gmail.com> wrote:
>
>
>
> > On Feb 4, 5:01=A0am, Rune Allnor <all...@tele.ntnu.no> wrote:
>
> > > On 4 Feb, 09:34, "blaž " <blaz.kr...@siol.net> wrote:
>
> > > > Hello to you all,
>
> > > > i've been struggling for a while with my first mex file. Basically =
it is a simlpe program with 2 for loops, one along i and one along j. If so=
me condition is satisfied i need to remember the specific i,j pair. So in t=
he end i want to have a [? x 2] matrix. The complete code is written bellow=
.. I am positive that the problem is with mxRealloc function, but i just can=
't make it work properly.
>
> > > Is this C or C++? If C++, use the std::stack<> container to store
> > > intermediate results, and then allocate the necessary space for
> > > the return variable at the very end, when you know how many
> > > elements will be returned.
>
> > > Apart from that, *never* base variable names on the single letters
> > > 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> > > alone or in conjunction with only numbers. At some point you will
> > > inevitably confuse them with the digits 1 or 0. *Only* use those
> > > letters in textual labels, where it is obvious that they are letters
> > > and not =A0numbers.
>
> > > Depending on the font settings in your code editor, you might not
> > > be able to see the difference between the letters and numbers,
> > > making for bugs that are extremely hard to track down, unless
> > > it is obvious from the context what they are.
>
> > > Rune
>
> > Why std::stack?
>
> Because of the semantics. The OP clearly does not know a lot
> of C(++), so any talk about vectors would likely confuse him
> about pre-allocating space etc, the same problems he tries
> to solve with the code he posted.
>
> > IMHO std::vector or std::deque would be a much better
> > choice in his case since they'll allow him to retrieve the matches in
> > the order they were found.
>
> The concept - and semantics - of a stack is that one does not
> care about memory allocations. They are taken care of internally.
> Once you know the number of elements present, you can allocate
> the necessary space in the vector, and fill in the elements
> in the order they are popped from the stack, whihf is the reverse
> order of the one theyr were found. Start filling in the elements
> at the end of the vector, and work your way towards the start.
>
> The end result is a list of hits, in the order they were found.
>
> Rune
I know we're arguing personal programming preferences here, but vector
doesn't require you to reserve memory either. In fact, in all
likelihood, your STL stack implementation is just an adapter
implemented around a vector object. I do agree with you that because
of the very limited interface that stack exposes it might be easier
for a beginner to work with, but vector isn't that hard to get a grasp
of either.
- Ashish
|
|
0
|
|
|
|
Reply
|
ashish.sadanandan (750)
|
2/4/2010 5:46:14 PM
|
|
On 4 Feb, 18:46, Praetorian <ashish.sadanan...@gmail.com> wrote:
> On Feb 4, 7:30=A0am, Rune Allnor <all...@tele.ntnu.no> wrote:
>
>
>
>
>
> > On 4 Feb, 15:22, Praetorian <ashish.sadanan...@gmail.com> wrote:
>
> > > On Feb 4, 5:01=A0am, Rune Allnor <all...@tele.ntnu.no> wrote:
>
> > > > On 4 Feb, 09:34, "blaž " <blaz.kr...@siol.net> wrote:
>
> > > > > Hello to you all,
>
> > > > > i've been struggling for a while with my first mex file. Basicall=
y it is a simlpe program with 2 for loops, one along i and one along j. If =
some condition is satisfied i need to remember the specific i,j pair. So in=
the end i want to have a [? x 2] matrix. The complete code is written bell=
ow. I am positive that the problem is with mxRealloc function, but i just c=
an't make it work properly.
>
> > > > Is this C or C++? If C++, use the std::stack<> container to store
> > > > intermediate results, and then allocate the necessary space for
> > > > the return variable at the very end, when you know how many
> > > > elements will be returned.
>
> > > > Apart from that, *never* base variable names on the single letters
> > > > 'l' (lowercase 'L'), 'I' (uppercase 'i') or 'O' (uppercase 'o'),
> > > > alone or in conjunction with only numbers. At some point you will
> > > > inevitably confuse them with the digits 1 or 0. *Only* use those
> > > > letters in textual labels, where it is obvious that they are letter=
s
> > > > and not =A0numbers.
>
> > > > Depending on the font settings in your code editor, you might not
> > > > be able to see the difference between the letters and numbers,
> > > > making for bugs that are extremely hard to track down, unless
> > > > it is obvious from the context what they are.
>
> > > > Rune
>
> > > Why std::stack?
>
> > Because of the semantics. The OP clearly does not know a lot
> > of C(++), so any talk about vectors would likely confuse him
> > about pre-allocating space etc, the same problems he tries
> > to solve with the code he posted.
>
> > > IMHO std::vector or std::deque would be a much better
> > > choice in his case since they'll allow him to retrieve the matches in
> > > the order they were found.
>
> > The concept - and semantics - of a stack is that one does not
> > care about memory allocations. They are taken care of internally.
> > Once you know the number of elements present, you can allocate
> > the necessary space in the vector, and fill in the elements
> > in the order they are popped from the stack, whihf is the reverse
> > order of the one theyr were found. Start filling in the elements
> > at the end of the vector, and work your way towards the start.
>
> > The end result is a list of hits, in the order they were found.
>
> > Rune
>
> I know we're arguing personal programming preferences here, but vector
> doesn't require you to reserve memory either. In fact, in all
> likelihood, your STL stack implementation is just an adapter
> implemented around a vector object. I do agree with you that because
> of the very limited interface that stack exposes it might be easier
> for a beginner to work with, but vector isn't that hard to get a grasp
> of either.
Again, the problem the OP presented indicates to me that
the different semantics concerning vectors and stacks midht
make a significant difference.
As for stacks being possible to implement in terms of vectors,
yes, you are right. It is possible. But even so, people prefer
to use stacs as opposed to vectors wherever the semantics of
the problem inodcates a stack might be appropriate.
I know this is a no-no what matlab is concerned, but the main
focus in C++ these days is to write code that makes sense to
the human being. The rationale is that a program is read more
often that it is written. So it pays in the long run to write
code that makes sense and is understandable to the human being.
Rune
|
|
0
|
|
|
|
Reply
|
allnor (8474)
|
2/4/2010 5:55:06 PM
|
|
|
9 Replies
38 Views
(page loaded in 0.237 seconds)
|