double の行列を読み取り、その値を処理し、それらを新しい行列に挿入する必要があります。その次元の 1 つは最初は不明です。
静的メモリ割り当てでは、私のコードは次のとおりです。
#include <mex.h>
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
const mxArray *I = prhs[0];
double *indata = mxGetPr(I);
double *submtx = mxGetPr(prhs[1]);
const int *size = mxGetDimensions(I);
int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
int xi = size[0], yi = size[1], zi = size[2];
int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
int out_rows = xp*yp*zp;
int out_cols = numsubmtx;
mxArray *out = mxCreateDoubleMatrix( out_rows, out_cols, mxREAL );
double *outdata = mxGetPr(out);
int submtx_counter = 0;
for( int z_offset = 0; ...; z_offset++ ){
for( int y_offset = 0; ...; y_offset++ ){
for( int x_offset = 0; ...; x_offset++ ){
int row = 0;
for( int z_counter = 0; ...; z_counter++ ){
for( int y_counter = 0; ...; y_counter++ ){
for( int x_counter = 0; ...; x_counter++ ){
outdata[submtx_counter*out_rows + row] =
indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
++row;
}}}
++submtx_counter;
}}}
plhs[0] = out;
}
動的バージョンでは、 の値がわからないため、 の値の条件が満たされたときout_cols
に再割り当てする必要があります。*out
indata
私の考えは次のようなものです:
#include <mex.h>
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
const mxArray *I = prhs[0];
double *indata = mxGetPr(I);
double *submtx = mxGetPr(prhs[1]);
const int *size = mxGetDimensions(I);
int xp = (int)submtx[0], yp = (int)submtx[1], zp = (int)submtx[2];
int xi = size[0], yi = size[1], zi = size[2];
int numsubmtx = (xi - xp + 1)*(yi - yp + 1)*(zi - zp + 1);
int out_rows = xp*yp*zp;
//int out_cols = numsubmtx; NOW UNKNOWN!
mxArray *out = NULL;
double *outdata = mxGetPr(out);
int submtx_counter = 0;
for( int z_offset = 0; ...; z_offset++ ){
for( int y_offset = 0; ...; y_offset++ ){
for( int x_offset = 0; ...; x_offset++ ){
int row = 0;
double condition=0;
for( int z_counter = 0; ...; z_counter++ ){
for( int y_counter = 0; ...; y_counter++ ){
for( int x_counter = 0; ...; x_counter++ ){
condition += indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ]/(xp*yp*zp);
++row;
}}}
if(coundition>0.5){
out = mxRealloc(out, (submtx_counter+1)*out_rows*sizeof(double));
double *outdata = mxGetPr(out);
int row = 0;
for( int z_counter = 0; ...; z_counter++ ){
for( int y_counter = 0; ...; y_counter++ ){
for( int x_counter = 0; ...; x_counter++ ){
outdata[submtx_counter*out_rows + row] = indata[ (x_offset+x_counter) + (y_offset+y_counter)*xi + (z_offset+z_counter)*xi*yi ];
++row;
}}}
++submtx_counter;
}
}}}
plhs[0] = out;
}
なにが問題ですか?