How do you create a matrix Aij where each value is determined by a formula?
39 views (last 30 days)
I need to define a matrix A where each element (aij) is defined by a formula.
For example, A = aij and aij = 1/(i+j-1) where i,j = 1,2,...,n. And I'll need to do analysis involving this matrix with various values of n (up to 20,000).
Any suggestions for how to define matrix A here?
Peter O on 2 Jul 2021
It sounds like a for loop would build your matrix nicely:
n = 2000;
sz_i = n;
sz_j = n;
A = zeros(sz_i, sz_j); % Preallocate your memory
A(ix, jx) = 1/(ix+jx-1);
Or is the formula more complex than what you've posted? If you have a general expression, you could wrap that by the loop and pass the index arguments.
For a one-off on a modern machine, this is plenty fast to construct, but you might hit some memory trouble at larger n. It's not sparse. It seems like the analysis is well-suited to a parallel implementation. How many values of n are we talking about and how complex is the analysis? Do the different size A's need to interact?