speed up a matlab code by replacing inv(X)*Y with X\Y in 3D dimensions

7 views (last 30 days)
hi,
I want to speed up my code by replacing inv(X)*Y with X\Y. The main issue is that X is a 3D matrix. In details, i have the following matrixes:
A = 380; B = 380; C = 200; D=1;
X = rand(A,B,C) +1i*rand(A,B,C); X2 = zeros(A,B,C);
tic;
for m=1:C
X2(:,:,m) = inv(X(:,:,m));
end
toc; % inverting the matrix X costs about 4.44 ~ 4.74 sec
Y = rand(A,1) + 1i*rand(A,1);
Z = reshape(reshape(permute(X2, [2 1 3]), [A B*C]), [B A*C]).' * Y;
Z = permute(reshape(Z.',[D A C]),[2 1 3]);
Output = zeros(A,C);
Output(:,:) = Z(:,1,:);
Basically i'm doing Z = inv(X)*Y on a 3D scale, Is there any simpler way to skip the for loop and use \ inside the Z= line without the need of X2=inv(X) and for ??
X, Y, X2 and Z are complex matrixes
Thanks in advance.

Answers (3)

Andrei Bobrov
Andrei Bobrov on 11 Sep 2012
A = 380;
B = 380;
C = 200;
X = rand(A,B,C) + 1i*rand(A,B,C);
Y = rand(A,1) + 1i*rand(A,1);
Z = zeros(A,C);
for m=1:C
Z(:,m) = X(:,:,m)\Y;
end

Sean de Wolski
Sean de Wolski on 11 Sep 2012
Please look into Tim Davis' comments/solutions here: Loren:Purpose of Inv

Jan
Jan on 11 Sep 2012

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!