In MATLAB, I have an array (512x512x512) of matrices (3x3). That is, I have an array of size (512x512x512x3x3) with 3x3 matrices as elements. I would like to take the matrix inverse at each element of the array. Is there a way I could go about doing this without iterating through the three dimensions? For example, the "brute-force" method of doing this would be the following pseudo-code:
CoB_inv_size = size(CoB);
CoB_inv = zeros(CoB_inv_size); % array of size (512x512x512x3x3)
for i = 1:CoB_inv_size(1) % 512
for j = 1:CoB_inv_size(2) % 512
for k = 1:CoB_inv_size(3) % 512
CoB_inv(i,j,k,:,:) = inv(CoB(i,j,k,:,:));
end
end
end
The array CoB
above is an array of change-of-basis matrices (where the columns are eigenvectors). I need the inverses of these matrices which will be utilized along with another diagonal matrix to obtain an array of matrices in their original basis using M=SDS^(-1)
. S
and S^(-1)
are represented by CoB
and CoB_inv
respectively. Is there a way to perform the above faster or more efficiently in MATLAB?
arrayfun
function applies a function to every element of a matrix/array. Although, you could use the workaround and create an array of cells that stores your 3x3 matrices and then use thecellfun
-function withcellfun(@inv,YourCellArray)
– max