开发者

4 dimensional matrix

I need to use 4 dimensional matrix as an accumulator for voting 4 parameters. every parameters vary in the range of 1~300. for that, I define Acc = zeros(300,300,300,300) in MATLAB. and somewhere for example, I used:

开发者_如何学Python
 Acc(4,10,120,78)=Acc(4,10,120,78)+1

however, MATLAB says some error happened because of memory limitation.

??? Error using ==> zeros
 Out of memory. Type HELP MEMORY for your options.

in the below, you can see a part of my code:

I = imread('image.bmp'); %I is logical 300x300 image.
Acc = zeros(100,100,100,100);
for i = 1:300
for j = 1:300
    if I(i,j)==1
        for x0 = 3:3:300
            for y0 = 3:3:300
                for a = 3:3:300
                        b = abs(j-y0)/sqrt(1-((i-x0)^2) / (a^2));
                        b1=floor(b/3);
                        if b1==0
                            b1=1;
                        end
                        a1=ceil(a/3);
                        Acc(x0/3,y0/3,a1,b1) = Acc(x0/3,y0/3,a1,b1)+1;                        
                end
            end
        end
    end
end
end


As @Rasman mentioned, you probably want to use a sparse representation of the matrix Acc.

Unfortunately, the sparse function is geared toward 2D matrices, not arbitrary n-D.

But that's ok, because we can take advantage of sub2ind and linear indexing to go back and forth to 4D.

Dims = [300, 300, 300, 300];   % it will be a 300 by 300 by 300 by 300 matrix
Acc = sparse([], [], [], prod(Dims), 1, ExpectedNumElts);

Here ExpectedNumElts should be some number like 30 or 9000 or however many non-zero elements you expect for the matrix Acc to have. We notionally think of Acc as a matrix, but actually it will be a vector. But that's okay, we can use sub2ind to convert 4D coordinates into linear indices into the vector:

ind = sub2ind(Dims, 4, 10, 120, 78);
Acc(ind) = Acc(ind) + 1;

You may also find the functions find, nnz, spy, and spfun helpful.


edit: see lambdageek for the exact same answer with a bit more elegance.

The other answers are helping to guide you to use a sparse mat instead of your current dense solution. This is made a little more difficult since current matlab doesn't support N-dimensional sparse arrays. One implementation to do this is

replace

 zeros(100,100,100,100)

with

sparse(100*100*100*100,1)

this will store all your counts in a sparse array, as long as most remain zero, you will be ok for memory.

then to access this data, instead of:

 Acc(h,i,j,k)=Acc(h,i,j,k)+1

use:

 index = h+100*i+100*100*j+100*100*100*k
 Acc(index,1)=Acc(index,1)+1


See Avoiding 'Out of Memory' Errors

Your statement would require more than 4 GB of RAM (Around 16 Gigs, to be specific).

Solutions to 'Out of Memory' problems fall into two main categories:

  • Maximizing the memory available to MATLAB (i.e., removing or increasing limits) on your system via operating system selection and system configuration. These usually have the greatest overall applicability but are potentially the most disruptive (e.g. using a different operating system). These techniques are covered in the first two sections of this document.

  • Minimizing the memory used by MATLAB by making your code more memory efficient. These are all algorithm and application specific and therefore are less broadly applicable. These techniques are covered in later sections of this document.

In your case later seems to be the solution - try reducing the amount of memory used / required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜