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.
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;
answer = A*x(1:N)
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) ).