Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 将滑块重新排列为3D阵列的列(3D中的IM2COL)-MATLAB_Performance_Matlab_Multidimensional Array_Vectorization - Fatal编程技术网

Performance 将滑块重新排列为3D阵列的列(3D中的IM2COL)-MATLAB

Performance 将滑块重新排列为3D阵列的列(3D中的IM2COL)-MATLAB,performance,matlab,multidimensional-array,vectorization,Performance,Matlab,Multidimensional Array,Vectorization,给定一个矩阵a(mxnxc)(c可以是任意的),我想在步长为d的滑动窗口方案中采样面片(pxp),并将所有pxc面片重新排列为向量。 我可以在嵌套for循环中完成,但这非常耗时。如何快速做到这一点?可以再次扩展3D阵列案例,以解决您的案例 现在,对这个问题有两种可能的解释: 提取大小为p x p的块,作为每个向量,对整个2D切片执行此操作,然后对3D中的所有切片重复此操作,从而产生3D输出 将大小为pxpxc的块收集为每个向量,并在整个阵列中以滑动方式进行此操作,从而产生2D输出 这两种解释

给定一个矩阵a(mxnxc)(c可以是任意的),我想在步长为d的滑动窗口方案中采样面片(pxp),并将所有pxc面片重新排列为向量。 我可以在嵌套for循环中完成,但这非常耗时。如何快速做到这一点?

可以再次扩展3D阵列案例,以解决您的案例

现在,对这个问题有两种可能的解释:

  • 提取大小为
    p x p
    的块,作为每个向量,对整个
    2D
    切片执行此操作,然后对
    3D
    中的所有切片重复此操作,从而产生
    3D
    输出

  • 将大小为
    pxpxc
    的块收集为每个向量,并在整个阵列中以滑动方式进行此操作,从而产生
    2D
    输出

这两种解释分别作为
im2col\u 3D\u sliding\u v1
im2col\u 3D\u sliding\u v2
实现,并在下面列出

im2col\U 3D\U滑动\U v1:

function out = im2col_3D_sliding_v1(A,blocksize,stepsize)

%// Store blocksizes
nrows = blocksize(1);
ncols = blocksize(2);

%// Store stepsizes along rows and cols
d_row = stepsize(1);
d_col = stepsize(2);

%// Get sizes for later usages
[m,n,r] = size(A);

%// Start indices for each block
start_ind = reshape(bsxfun(@plus,[1:d_row:m-nrows+1]',[0:d_col:n-ncols]*m),[],1); %//'

%// Row indices
lin_row = permute(bsxfun(@plus,start_ind,[0:nrows-1])',[1 3 2]);  %//'

%// 2D linear indices
lidx_2D = reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]);

%// 3D linear indices
lidx_3D = bsxfun(@plus,lidx_2D,m*n*permute((0:r-1),[1 3 2]));

%// Get linear indices based on row and col indices and get desired output
out = A(lidx_3D);

return;
function out = im2col_3D_sliding_v2(A,blocksize,stepsize)

%// Store blocksizes
nrows = blocksize(1);
ncols = blocksize(2);

%// Store stepsizes along rows and cols
d_row = stepsize(1);
d_col = stepsize(2);

%// Get sizes for later usages
[m,n,r] = size(A);

%// Start indices for each block
start_ind = reshape(bsxfun(@plus,[1:d_row:m-nrows+1]',[0:d_col:n-ncols]*m),[],1); %//'

%// Row indices
lin_row = permute(bsxfun(@plus,start_ind,[0:nrows-1])',[1 3 2]);  %//'

%// 2D linear indices
lidx_2D = reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]);

%// 3D linear indices
lidx_3D = bsxfun(@plus,permute(lidx_2D,[1 3 2]),m*n*(0:r-1));

%// Final 2D linear indices
lidx_2D_final = reshape(lidx_3D,[],size(lidx_2D,2));

%// Get linear indices based on row and col indices and get desired output
out = A(lidx_2D_final);

return;
im2col\U 3D\U滑动\U v2:

function out = im2col_3D_sliding_v1(A,blocksize,stepsize)

%// Store blocksizes
nrows = blocksize(1);
ncols = blocksize(2);

%// Store stepsizes along rows and cols
d_row = stepsize(1);
d_col = stepsize(2);

%// Get sizes for later usages
[m,n,r] = size(A);

%// Start indices for each block
start_ind = reshape(bsxfun(@plus,[1:d_row:m-nrows+1]',[0:d_col:n-ncols]*m),[],1); %//'

%// Row indices
lin_row = permute(bsxfun(@plus,start_ind,[0:nrows-1])',[1 3 2]);  %//'

%// 2D linear indices
lidx_2D = reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]);

%// 3D linear indices
lidx_3D = bsxfun(@plus,lidx_2D,m*n*permute((0:r-1),[1 3 2]));

%// Get linear indices based on row and col indices and get desired output
out = A(lidx_3D);

return;
function out = im2col_3D_sliding_v2(A,blocksize,stepsize)

%// Store blocksizes
nrows = blocksize(1);
ncols = blocksize(2);

%// Store stepsizes along rows and cols
d_row = stepsize(1);
d_col = stepsize(2);

%// Get sizes for later usages
[m,n,r] = size(A);

%// Start indices for each block
start_ind = reshape(bsxfun(@plus,[1:d_row:m-nrows+1]',[0:d_col:n-ncols]*m),[],1); %//'

%// Row indices
lin_row = permute(bsxfun(@plus,start_ind,[0:nrows-1])',[1 3 2]);  %//'

%// 2D linear indices
lidx_2D = reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]);

%// 3D linear indices
lidx_3D = bsxfun(@plus,permute(lidx_2D,[1 3 2]),m*n*(0:r-1));

%// Final 2D linear indices
lidx_2D_final = reshape(lidx_3D,[],size(lidx_2D,2));

%// Get linear indices based on row and col indices and get desired output
out = A(lidx_2D_final);

return;
样本运行

(一) 输入阵列:

>> A
A(:,:,1) =
    23   109    63     1    37   153
   110    31   201    57    69   230
    66   127    19     1    45   240
    76   181   101    49    36    57
A(:,:,2) =
   124    18   244     2   141    95
    96   112   110   174    56   228
   134    45   246   181   197   219
    68     7   195   165    59   103
(二) 输入参数:

>> blocksize = [2,3]; %// blocksize along rows, cols
>> stepsize = [2,2];  %// stepsize along rows, cols
(三) 两个版本的输出:

>> im2col_3D_sliding_v1(A,blocksize,stepsize)
ans(:,:,1) =
    23    66    63    19
   110    76   201   101
   109   127     1     1
    31   181    57    49
    63    19    37    45
   201   101    69    36
ans(:,:,2) =
   124   134   244   246
    96    68   110   195
    18    45     2   181
   112     7   174   165
   244   246   141   197
   110   195    56    59

   >> im2col_3D_sliding_v2(A,blocksize,stepsize)
ans =
    23    66    63    19
   110    76   201   101
   109   127     1     1
    31   181    57    49
    63    19    37    45
   201   101    69    36
   124   134   244   246
    96    68   110   195
    18    45     2   181
   112     7   174   165
   244   246   141   197
   110   195    56    59

因此,输出将是
2D
,行数=p*p*c,对吗?是的,你是对的。@DivakarI请看。因此,下面发布的解决方案中的
im2col\u 3D\u slideing\u v2
有望奏效。我会保留另一个版本,未来的读者可能会从中受益。是的,im2col\U 3D\U Slideing\U v2是一个很好的解决方案。感谢您的努力。它不应该也有一个参数化的步长
d
?@kkuilla是的,我在等待OP的输出大小澄清,以合并步长。刚刚在编辑中添加了参数:)很好的解决方案。谢谢。这个过程可以根据你的要求颠倒吗?也就是说,在采样位置累积补丁,然后除以累积时间得到概率输出?@LiangXiao我认为将其作为一个单独的问题发布是有意义的。