How do you create a matrix Aij where each value is determined by a formula?

176 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?

Answers (1)

Peter O
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
for ix=1:sz_i
for jx=1:sz_j
A(ix, jx) = 1/(ix+jx-1);
end
end
disp(size(A))
2000 2000
disp(A(1:4,1:4))
1.0000 0.5000 0.3333 0.2500 0.5000 0.3333 0.2500 0.2000 0.3333 0.2500 0.2000 0.1667 0.2500 0.2000 0.1667 0.1429
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?
  2 Comments
Peter O
Peter O on 3 Jul 2021
Your solution works fine and is elegant!
In general, FOR loops are fast and easy to construct, and the implementation above would use a third of the memory. But they're also generally slower than some of MATLAB's vectorizable operations, which you are putting to excellent use there. I wasn't sure if you were targeting that formula exactly or had some obfuscation, so I gave a generalized answer that would work for most anything.
For n of 20,000, the matrix is 4 million elements and dense. At 8 bytes per element assuming a double, you're probably fine. That's roughly 3 gigabytes per matrix, so about 9 GB in working memory with ndgrid above, since you'll get full matrices for those i and j outputs. On a modern machine for computation, you should have plenty of space. You can clear the i and j matrices afterwards to free the memory.
Regardless of initialization, you may run into trouble when you solve. MATLAB's backslash operator is highly optimized, but there's a chance it will still need to borrow some disk space while it solves, which will slow the solver. If you've got enough RAM and you aren't running other programs it will be fine. Some other considerations to think about from a numerical linear algebra perspective are the condition number of the matrix and how you're measuring the "accuracy" of the solution with an error norm (consider using the L2 norm for the vector).
This sort of thing is MATLAB's bread and butter, and Cleve Moler (MATLAB's creator) writes often on the subject. I recommend you check out his blog: https://blogs.mathworks.com/cleve/ as well as some of the documentation of MATLAB's matrix solvers: https://www.mathworks.com/help/matlab/ref/mldivide.html

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!