Saji Ren
Saji Ren

Reputation: 115

Why using repmat() for expanding array?

I want to load a csv file to Matlab using testread(), since the data in it has more than 2 million records, so I should preallocate the array for those data.

Suppose I cannot know the exact length of arrays, the docs of MATLAB v6.5 recommend me to use repmat() for my expanding array. The original words in the doc is below:

"In cases where you cannot preallocate, see if you can increase the size of your array using the repmat function. repmat tries to get you a contiguous block of memory for your expanding array".

I really don't know how to use the repmat for expanding?

Does it mean by estimating a rough number of the length for repmat() to preallocating, and then remove the empty elements?

If so, how is that different from preallocating using zeros() or cell()?

Upvotes: 0

Views: 258

Answers (1)

ronalchn
ronalchn

Reputation: 12335

The documentation also says:

When you preallocate a block of memory to hold a matrix of some type other than double, it is more memory efficient and sometimes faster to use the repmat function for this.

The statement below uses zeros to preallocate a 100-by-100 matrix of uint8. It does this by first creating a full matrix of doubles, and then converting the matrix to uint8. This costs time and uses memory unnecessarily.

A = int8(zeros(100));

Using repmat, you create only one double, thus reducing your memory needs.

A = repmat(int8(0), 100, 100);

Therefore, the advantage is if you want a datatype other than doubles, you can use repmat to replicate a non-double datatype.

Also see: http://undocumentedmatlab.com/blog/preallocation-performance, which suggests:

data1(1000,3000) = 0

instead of:

data1 = zeros(1000,3000)

to avoid initialisation of other elements.

As for dynamic resizing, repmat can be used to concisely double the size of your array (a common method which results in amortized O(1) appends for each element):

data = [0];
i = 1;
while another element
    ...
    if i > numel(data)
      data = repmat(data,1,2); % doubles the size of data
    end
    data(i) = element
    i = i + 1;
end

And yes, after you have gathered all your elements, you can resize the array to remove empty elements at the end.

Upvotes: 3

Related Questions