Asked by MosArt
on 3 Apr 2014

I am running some code in which I am using and modifying a fairly large (order 1e3-1e4) square matrix that constantly changes in size. So, an increase in size would correspond to size [NxN] --> [(N+1)x(N+1)], and a decrease would be [NxN] --> [(N-1)x(N-1)]. Upon modifying this matrix, I'm also using it for operations that use BLAS routines.

Preallocating this matrix would be a must for maximizing speed, of course. I'm aware of the largest size that this matrix can get, so (AFAIK), one option would be to preallocate with A = zeros(Nmax,Nmax), and then work with the submatrix A(indices,indices). However, the work I'm doing with this "submatrix" requires further indexing (even double indexing at a couple instances) within the matrix. This rather obfuscates the code.

Is there a way to initialize some kind of declaration that preallocate a block of memory for A in such a way that force me to use a submatrix of A as the matrix I'm actually interested in using.

It seems that one option may be some MATLAB macro-like hack, in which I would have something like

define A(.,.) as A(q(.),q(.)), with q = 1:1:length(A)

Aside from something like that, is there any good way to accomplish this? It would be nice to take care of this allocation at initialization, and not worry about changing the rest of the code. Most things that I find online discuss only the most basic preallocation schemes.

Thanks

EDIT: I guess, to clarify.. consider the following.

%Init A = rand(N,N); x = rand(N,1);

for i = 1:1:100

if (some condition) A(1:N-1,1:N-1) = A(1:N-1,1:N-1) - A(1:N-1,N)*A(1:N-1,N)'; A(:,N) = []; A(N,:) = []; N = N-1; end

answer = A*x(1:N);

if (some other condition)

v = A*rand(N,1); a = rand(1); A = [A,v; v',a]; N = N+1;

end

answer = A*x(1:N)

end

end

This is poor memory management, since all of A gets new memory assigned every time. What is a better way to do this? I'd simply like to 'tell' MATLAB, "I want THIS much memory for A, and even when I downsize it, I want that space reserved for it when I expand it again." (But I still want to refer to A as 'A'... not A(1:currentsize,1:currentsize) ).

*No products are associated with this question.*

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/124274#comment_205803

Are you doing row and column operations when you access, or just indexing by scalars?

I am having difficulty understanding what you mean about "in such a way that force me to use a submatrix of A as the matrix I'm actually interested in using" ?

## MosArt (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/124274#comment_205873

I'm doing row and column operations. Let me give an example.

Throughout the process, I may do something like, A = [ [A;v(1:end-1)'], v], corresponding to expanding the matrix by a row and column (v is a column vector here.) Or, I may do something like A = A(1:end-1,1:end-1) - A(:,end)*A(:end)' (so, I'm grabbing a portion of the matrix, but using the rest of it for modifying this portion as well.) After these modifications, I may use the matrix for BLAS routines, such as matrix-vector products, prior to modifying them again.

After this, I use (the modified) A for basic BLAS operations such as matrix-vector products, and may also be interested in using portions of A for other basic routines, for example: R = A(:,[3 4 5])'*A(:,[3 4 5]).

The way I understand it, the operations above that involve shrinking and expanding the matrix A, cause MATLAB to reallocate space for the entire matrix. One option (I would think) is to let A = zeros(max,max), and no longer work with A, but with blocks of it. So, instead of A = something, I have A(1:dim,1:dim) = something. This seems somewhat messy to propagate through the code, though. Does that make sense? So, I would like to preallocate memory for the matrix A, but still keep referencing it as A, instead of blocks of A, even upon size changes.

Am I over-thinking this? Or expecting too much?