What is the alternative to predefined arrays in MATLAB?

Possible duplicate:
Robust data structure in MATLAB

So, in my current MATLAB script, I have a very large growing array of indefinite size. There is nothing I can do about it at the moment, because if I actually predetermine it will require many times more memory than I need (the maximum possible number of values ​​is 640 per pixel, but usually it's something like 2- lines 5).

Usually in this case I would use a vector in C ++ or something else where it grows exponentially with respect to a given capacity. But I think that matrices in Matlab are starting to fragment much faster than targeted C ++ vectors.

What do you guys think is the best alternative to something like that? Or should I just stick to normal arrays and hope that adding about 100K elements works consistently?

Thanks in advance.

+4
source share
4 answers

You can try what std::vectorit does when redistributing elements - it doubles its capacity each time it is filled, which has an amortized cost of O (1).

Test:

function test_MAT_vector

    LIM = 5e4;
    %%# Normal
    tic;
    A = zeros(1,1);
    for i = 1:LIM
        A(end+1) = i;
    end
    toc

    %%# std::vector clone
    tic;
    B = zeros(1,1);
    for i = 1:LIM
        if(i > numel(B))
            B = [B;zeros(numel(B),1)];
        end
        B(i) = i;
    end
    toc

end

Conclusion:

Elapsed time is 3,489820 seconds.

Elapsed time is 0.006710 seconds.

Cell usage

%%# cell
tic;
A = cell(1,1);
for i = 1:LIM
    A(end+1) = {i};
end
toc

The elapsed time is 2.740792 seconds.

+6
source

, , , , . , . ( , , .)

, , , O (N ^ 2). , , .

, , , . , . , , . , , . .

- . , , . cell2mat. - O (N ^ 2), , .

, , , . , , 10000 . . . , . , . , , , .

, , , .

, File Exchange, . grow_array, , .

, , . , , growdata growdata2.

+10

, @Jacob.

( @woodchips ). , :

NUM = 50000;

%%# ========== MINE: ~0.07sec ==========
tic
BLOCK_SIZE = 2000;                           %# initial & increment size
listSize = BLOCK_SIZE;                       %# current list capacity
list = zeros(listSize, 2);                   %# actual list
listPtr = 1;                                 %# pointer to last free position
for i=1:NUM
    %# push items on list
    list(listPtr,:) = [rand rand];           %# store new item
    listPtr = listPtr + 1;                   %# increment position pointer
    %# add new block of memory if needed
    if( listPtr+(BLOCK_SIZE/10) > listSize ) %# less than 10%*BLOCK_SIZE free
        listSize = listSize + BLOCK_SIZE;    %# add new BLOCK_SIZE slots
        list(listPtr+1:listSize,:) = 0;
    end
end
list(listPtr:end,:) = [];                    %# remove unused slots
toc

%%# ========== PREALLOCATION (matrix): ~0.05sec ==========
tic
list = zeros(NUM,2);
for i=1:NUM
    list(i,:) = [rand rand];
end
toc

%%# ========== PREALLOCATION (cell): ~1.1sec ==========
tic
list = cell(NUM,1);
for i=1:NUM
    list{i} = [rand rand];
end
list = vertcat( list{:} );
toc

%%# ============ NO-PREALLOCATION (grow cell): ~5sec ========
tic
list = {};
for i=1:NUM
    list{i} = [rand rand];
end
list = vertcat( list{:} );
toc

%%# ============ NO-PREALLOCATION (grow matrix): ~24sec ========
tic
list = [];
for i=1:NUM
    list(i,:) = [rand rand];
end
toc

%%# ========== GROWDATA (by John D'Errico): ~3.3sec =========
tic
growdata                             %# The initialization call
for i = 1:NUM
    growdata( [rand rand] )          %# push items
end
list = growdata;                     %# unpacking step
toc
+2

, , , cell2mat ( , ).

, , ( 640, 2-5), cell2mat, .

, pack , ( ), cell2mat.

0

All Articles