How can recursive functions be written in matlab mex ?
조회 수: 1 (최근 30일)
이전 댓글 표시
Can anyone help me about writing recursive function in matlab mex ? I get error message and then I am told to shutdown and restart matlab. What is the problem ?
댓글 수: 3
Jan
2012년 12월 18일
It is better to add all information required to understand the problem by editing the question. If there is a larger number of comments, the inital ones are not visible as default, such that readers will understand the full problem after some interactions only. But the faster a reader can understand the question, the more likely is an answer.
답변 (1개)
Jan
2012년 12월 18일
편집: Jan
2012년 12월 18일
mxGetM replies a size_t variable, which is not an int under 64 bit. Better:
mwSize i, m, n;
It is a bad idea to replace the memory of plhs[0] using mxSetPr without clearing the original array. Although this does not crash Matlab immediately, this is prone to leak memory. It would be saver and more efficient to use the already allocated memory of plhs[0]:
data2 = (double *) mxGetPr(plhs[0]);
You did not show the code of "recurs()", so currently there is no recursion at all. Are you sure that you want to reply [1, 0, 0, ...], when the first inpout value is 1?
It is confusing, that you use "i" as index for the input, the output and the first element of the data. It is not wrong, but using a 0 directly would be more clear.
You do not have to inlcude "matrix.h", when "mex.h" is included already.
댓글 수: 8
Walter Roberson
2012년 12월 25일
Recursion usually does not speed up routines. Sometimes it makes it easier to write routines, but it seldom makes them any faster.
There are some computer languages in which particular forms of recursion ("tail recursion") are optimized, but MATLAB is not one of them.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!