看板 MATLAB 關於我們 聯絡資訊
我想要用mex做陣列相加再回傳到matlab 程式如下 %%%這是matlab code mex add.c; b = {1.1,1.1}; c = {2.2,2.2}; a = add(b, c); //這是 add.mex #include "mex.h" void mexFunction(int nlhs,mxArray *plhs[], int nrhs,const mxArray *prhs[]) { double *a, *b, *c; int i; plhs[0] = mxCreateDoubleMatrix(1, 2, mxREAL); a = mxGetPr(plhs[0]); b = mxGetPr(prhs[0]); c = mxGetPr(prhs[1]); for(i=0;i<2;i++){ a[i]=b[i]+b[i]; } } 以上我先把陣列大小寫死,但是我沒有得到正確的a值a={3.3,3.3},而是得到 a={1.669273906000000e-315,1.669273906000000e-315},請問我錯在哪裡 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.116.20.6 ※ 文章網址: http://www.ptt.cc/bbs/MATLAB/M.1408788124.A.FBF.html
sunev: 大括孤? 08/23 18:08
ofox6072: 什麼大括弧? 08/23 18:17
celestialgod: 你的b跟c是cell... b = {1.1, 1.1}... 08/23 18:27
celestialgod: 而且C的迴圈寫錯,應該是a[i] = b[i] + c[i]; 08/23 18:28
ofox6072: OK,但是我覺得應該是指標有問題 08/23 18:30
ofox6072: 他給出的答案感覺像是記憶體位置 08/23 18:31
ofox6072: 如過是a[i] = b[i] + b[i],也應該是a={2.2,2.2} 08/23 18:32
ofox6072: 請問你說得的cell是什麼意思 08/23 18:34
celestialgod: a={2.2, 2.2} 是assign a為cell,而非矩陣 08/23 20:34
celestialgod: b = [1.1, 1.1]才是assign matrix 08/23 20:34
ofox6072: 改成[ ]就可以了,感謝C大 08/23 21:00
ofox6072: 那如果要cell用mex相加回傳到matlab,有什麼想法嗎 08/23 21:02
celestialgod: doc mexGetCell看範例吧 08/24 06:25
celestialgod: create則看mxCreateCellArray 08/24 06:25