From: Kai Wang on
Hi,

My first time posting question here.

I have an array "A" with size (m X m X n), basically n concatenated square matrices. Is there any faster way to invert each matrix A(:,:,i) instead of using for loop such as:

for i=1:n
B(:,:,i)=inv(A(:,:,i));
end

Thanks!
From: James Tursa on
"Kai Wang" <coolarhoo(a)gmail.com> wrote in message <hqnico$atj$1(a)fred.mathworks.com>...
> Hi,
>
> My first time posting question here.
>
> I have an array "A" with size (m X m X n), basically n concatenated square matrices. Is there any faster way to invert each matrix A(:,:,i) instead of using for loop such as:
>
> for i=1:n
> B(:,:,i)=inv(A(:,:,i));
> end

Pre-allocate B first. Also, what are you using the inverses for? Usually using backslash is a better option if you are solving linear equations.

James Tursa
From: Bruno Luong on
You could use my FEX tool: http://www.mathworks.com/matlabcentral/fileexchange/24260-multiple-same-size-linear-solver

B=SliceMultiSolver(A,eye(m));

Bruno
From: Kai on
Thanks for the reply! I mainly use it to invert between covariance and precision matrices to do some statistical computation. From time to time I could figure out some way to avoid involving covariance or precision matrices explicitly. I was just wondering if there's any array manipulation trick or any one has written some function to do that without explicit for-loops, similar to how we avoid loops by vectorization in some other simple calculations.


"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hqnjvd$8p4$1(a)fred.mathworks.com>...
> "Kai Wang" <coolarhoo(a)gmail.com> wrote in message <hqnico$atj$1(a)fred.mathworks.com>...
> > Hi,
> >
> > My first time posting question here.
> >
> > I have an array "A" with size (m X m X n), basically n concatenated square matrices. Is there any faster way to invert each matrix A(:,:,i) instead of using for loop such as:
> >
> > for i=1:n
> > B(:,:,i)=inv(A(:,:,i));
> > end
>
> Pre-allocate B first. Also, what are you using the inverses for? Usually using backslash is a better option if you are solving linear equations.
>
> James Tursa