Invert a set of multiple matrices - efficiency & time issues
Show older comments
Hi guys, I've got a simple problem here; I need to invert matrices. They're all quite simple and friendly, and of the same size - 5x5. The slight problem is, there are around 15000 of them. So of course I could create an array of 15000x5x5 and run the loop-
for i = 1:end
inv_matrices_array(i,:,:) = inv( matrices_array(i,:,:) )
end
but... for 15000 matrices, that takes too much time. Surely I've missed a simpler, more elegant way to do this, haven't I? Or must I re-think my entire code?
Your help is much appreciated!
Answers (1)
Azzi Abdelmalek
on 24 Feb 2013
Edited: Azzi Abdelmalek
on 24 Feb 2013
It's not 15000x5x5 but 5x5x15000
%Your data
matrices_array=rand(5,5,15000);
n=size(matrices_array,3);
%First way
tic
inv_matrices_array=zeros(5,5,n); % Pre-allocate
for k=1:n
inv_matrices_array(:,:,k)=inv(matrices_array(:,:,k));
end
toc
%second way but not faster
tic
inv_matrices_array1{k}=arrayfun(@(x) inv(matrices_array(:,:,x)),1:n,'un',0);
toc
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!