我试图在下面的代码(来自一个a[++j]=*pr++;文件)中找出在MatLab中等效的是什么。我发现'pr‘是一个指向输入数组的第一个元素的指针,但是我无法理解j上发生的事情。有人能用简单的术语来解释在没有指针的情况下发生了什么吗?
rf3(mxArray *array_ext, mxArray *hs[]) {
double *pr, *po, a[16384], ampl, mean;
int tot_num, index, j, cNr;
mxArray *array_out;
tot_num = mxGetM(array_ext) * mxGetN(array_ext);
pr = (double *)mxGetPr(array_ext);
array_out = mxCreateDoubleMatrix(3, tot_num-1, mxREAL);
po = (double *)mxGetPr(array_out);
j = -1;
cNr = 1;
for (index=0; index<tot_num; index++) {
a[++j]=*pr++;
while ( (j >= 2) && (fabs(a[j-1]-a[j-2]) <= fabs(a[j]-a[j-1])) ) {
ampl=fabs( (a[j-1]-a[j-2])/2 );
switch(j)
{
case 0: { break; }
case 1: { break; }
case 2: {
mean=(a[0]+a[1])/2;
a[0]=a[1];
a[1]=a[2];
j=1;
if (ampl > 0) {
*po++=ampl;
*po++=mean;
*po++=0.50;
}
break;
}
default: {
mean=(a[j-1]+a[j-2])/2;
a[j-2]=a[j];
j=j-2;
if (ampl > 0) {
*po++=ampl;
*po++=mean;
*po++=1.00;
cNr++;
}
break;
}
}
}
}
for (index=0; index<j; index++) {
ampl=fabs(a[index]-a[index+1])/2;
mean=(a[index]+a[index+1])/2;
if (ampl > 0){
*po++=ampl;
*po++=mean;
*po++=0.50;
}
}
/* you can free the allocated memeory */
/* for array_out data */
mxSetN(array_out, tot_num - cNr);
hs[0]=array_out;
}发布于 2013-11-01 13:57:51
你特别问的是:
a[++j]=*pr++;J是在分配之前增加的。在python中,左手边应该是:
a[j+1] 然后,在接下来使用它之前,还需要增加j:
j += 1右边只访问当前位置,然后增加数组中的位置。在python中,您可能只需要为数组使用迭代器。
顺便说一句,你可能会发现很难对代码进行逐行的“翻译”。我建议将算法的步骤写下来,然后在python中重新处理,如果需要的话。
发布于 2013-11-01 13:52:17
下面是发生的事情:
j 1a[j]值赋值给pr指向的值pr按照这个顺序。
发布于 2013-11-01 13:58:09
在Python中,没有指针,所以如何翻译将取决于您决定如何表示pr。如果将指针视为列表pr = array_ext[:]的副本,则突出显示的行将类似于
j = j + 1
a[j] = pr.pop(0)为了提高效率(以及更接近C代码正在做的事情),您可以使用pr作为列表array_ext的索引,从0开始。然后,您突出显示的行是:
j = j + 1
a[j] = array_ext[pr]
pr = pr + 1https://stackoverflow.com/questions/19728069
复制相似问题