Question

I need to make a scilab / MATLAB program that averages the values of a 3D matrix in cubes of a given size(N x N x N).I am eternally grateful to anyone who can help me.

Thanks in advance

Était-ce utile?

La solution

In MATLAB, mat2cell and cellfun make a great team for working on N-dimensional non-overlapping blocks, as I think is the case in the question. An example scenario:

  • [IN]: A = [30x30x30] array
  • [IN]: bd = [5 5 5], size of cube
  • [OUT]: B = [6x6x6] array of block means

To accomplish the above, the solution is:

dims = [30 30 30]; bd = [5 5 5];
A = rand(dims);
f = floor(dims./bd);
remDims = mod(dims,bd); % handle dims that are not a multiple of block size
Ac = mat2cell(A,...
    [bd(1)*ones(f(1),1); remDims(1)*ones(remDims(1)>0)], ....
    [bd(2)*ones(f(2),1); remDims(2)*ones(remDims(2)>0)], .... 
    [bd(3)*ones(f(3),1); remDims(3)*ones(remDims(3)>0)] );
B = cellfun(@(x) mean(x(:)),Ac);

If you need a full size output with the mean values replicated, there is a straightforward solution involving the 'UniformOutput' option of cellfun followed by cell2mat.

If you want overlapping cubes and the same size output as input, you can simply do convn(A,ones(blockDims)/prod(blockDims),'same').

EDIT: Simplifications, clarity, generality and fixes.

Autres conseils

    N = 10; %Same as OP's parameter
    M = 10*N;%The input matrix's size in each dimensiona, assumes M is an integer multiple of N
    Mat = rand(M,M,M); % A random input matrix
    avgs = zeros((M/N)^3,1); %Initializing output vector

    l=1; %indexing
    for i=1:M/N %indexing 1st coord
        for j=1:M/N %indexing 2nd coord
            for k=1:M/N % indexing third coord
                temp = Mat((i-1)*N+1:i*N,(j-1)*N+1:j*N,(k-1)*N+1:k*N); %temporary copy
                avg(l) = mean(temp(:)); %averaging operation on the N*N*N copy
                l = l+1; %increment indexing
            end
        end
    end

The for loops and copying can be eliminated once you get the gist of indexing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top