如何用简洁命令构造由基矩阵幂次构成的分块矩阵?
Great question! When you need to construct a block matrix B = [A; A²; A³; ...; Aⁿ] from a base matrix A, there are cleaner, more elegant alternatives to explicit for loops. Here are my go-to approaches:
1. One-Liner with arrayfun + cell2mat
While you mentioned arrayfun, you can condense it into a single, readable line without a full loop. This is perfect for small to moderate values of n:
n = 3; % Replace with your desired positive integer A = [1, 2; -1, 3]; B = cell2mat(arrayfun(@(k) A^k, 1:n, 'UniformOutput', false));
How it works:
arrayfun(@(k) A^k, 1:n, 'UniformOutput', false)generates a cell array where each element isA^kforkfrom 1 ton.cell2matthen vertically stacks all these matrices into your desired block matrixB.
2. Efficient Recursive Approach (Cayley-Hamilton Theorem)
For larger values of n, computing each matrix power individually can be inefficient. Instead, use the Cayley-Hamilton Theorem, which states that any matrix satisfies its own characteristic equation. This lets us express higher powers of A as linear combinations of lower powers, reducing redundant calculations:
n = 5; % Example large n A = [1, 2; -1, 3]; % Get coefficients of A's characteristic polynomial char_poly = poly(A); % Initialize cell array to hold matrix powers powers = cell(1, n); powers{1} = A; if n >= 2, powers{2} = A^2; end % Recursively compute higher powers using Cayley-Hamilton for k = 3:n % Combine lower powers using the characteristic equation powers{k} = -char_poly(1:end-1) * cell2mat(powers(k-1:-1:k-length(char_poly)+1)); end % Stack into the final block matrix B = cell2mat(powers);
This method shines when n is large because it avoids recalculating matrix products from scratch for each power.
Quick Note on Element-Wise vs Matrix Powers
Just to clarify: since you mentioned scalar A.^(1:n), remember that A^k is matrix multiplication (what you need here), while A.^k is element-wise exponentiation—make sure you use the correct operator for your use case!
内容的提问来源于stack exchange,提问作者ThomasIsCoding

