4

サブマトリックスのコレクションを収集したい大きなマトリックスがあります。行列がNxNで、部分行列のサイズがMxMの場合、部分I=(N - M + 1)^2行列を収集します。言い換えると、元の行列の要素ごとに1つのMxM部分行列が必要であり、そのような行列の左上隅に配置できます。

これが私が持っているコードです:

for y = 1:I
    for x = 1:I
        index = (y - 1) * I + x;
        block_set(index) = big_mat(x:x+M-1, y:y+M-1)
    endfor
 endfor

a)間違っている場合、およびb)big_mat(x:x+M-1, y:y+M-1)2つのforループを必要とせずに、必要なものを取得できる式に何かがあることを意味する場合の出力。どんな助けでも大歓迎です

4

3 に答える 3

5

There seem to be a few things wrong in your code. Here's how I'd do it if I were to use the double loop:

M = someNumber;
N = size(big_mat,1); %# I assume big_mat is square here

%# you need different variables for maxCornerCoord and nSubMatrices (your I)
%# otherwise, you are going to index outside the image in the loops!
maxCornerCoord = N-M+1;
nSubMatrices = maxCornerCoord^2;

%# if you want a vector of submatrices, you have to use a cell array...
block_set = cell(nSubMatrices,1); 
%# ...or a M-by-M-by-nSubMatrices array...
block_set = zeros(M,M,nSubMatrices);
%# ...or a nSubMatrices-by-M^2 array
block_set = zeros(nSubMatrices,M^2);

for y = 1:maxCornerCoord
    for x = 1:maxCornerCoord
        index = (y - 1) * maxCornerCoord + x; 
        %# use this line if block_set is a cell array
        block_set{index} = big_mat(x:x+M-1, y:y+M-1);
        %# use this line if block_set is a M-by-M-by-nSubMatrices array
        block_set(:,:,index) = big_mat(x:x+M-1, y:y+M-1);
        %# use this line if block_set is a nSubMatrices-by-M^2 array
        block_set(index,:) = reshape(big_mat(x:x+M-1, y:y+M-1),1,M^2);
    endfor
 endfor

EDIT

I just saw that there is an implementation of im2col for Octave. Thus, you can rewrite the double-loop as

%# block_set is a M^2-by-nSubMatrices array
block_set = im2col(big_mat,[M,M],'sliding');

%# if you want, you can reshape the result to a M-by-M-by-nSubMatrices array
block_set = reshape(block_set,M,M,[]);

This is probably faster, and saves lots of digital trees.

于 2010-04-26T21:33:48.253 に答える
1

Mathematicaの場合:このコードは、各要素がMxMの行列であり、元の行列の各要素がそのような行列の左上隅にある行列を作成します。

右と下に沿った行列要素にはxが埋め込まれます。

Partition[big_mat, {M, M}, {1, 1}, {1, 1}, x]

例: 代替テキストhttp://img130.imageshack.us/img130/6203/partitionf.png

x引数を省略すると、定期的に自動的にサンプリングされます。

于 2010-04-26T23:51:04.757 に答える
0

出力が間違っている場合は、割り当てが原因である可能性があります。行列をベクトル位置に割り当てようとしています。使ってみてください

block_set(:,:,index) = big_mat(x:x+M-1, y:y+M-1)

代わりは。

于 2010-04-26T20:39:43.460 に答える