Is it possible to solve multiple linear systems of equations in parallel with one matrix operation?

2 次查看(过去 30 天)
I'm wondering if there's a way to do the following calculations in one go (i.e. without the for loop).
V = nan([na nv]);
for i=1:nv
% Solve homogenous system of equations
V(:,i) = [Aa-L(i)*Ia] \ (-Ba*Phi(i));
end
For the purposes of this example, let's consider the variables having the following values:
Aa = [ 0.819 0;
-0.544 1];
na = size(Aa,2);
Ba = [1; 0];
Phi = [1 1];
L = [0.7515 0.7165];
nv = numel(L);
Ia = eye(na);
Which yields the solution:
V =
-14.8148 -9.7561
-32.4316 -18.7207

采纳的回答

Paul
Paul 2022-7-14
Hi Bill,
pagemldivide introduced in 2022a can do the trick. Whether or not this is really better than the loop ....
Aa = [ 0.819 0;
-0.544 1];
Ba = [1; 0];
Phi = [1 1];
L = [0.7515 0.7165];
V = reshape(pagemldivide(Aa - reshape(L,1,1,[]) .* eye(size(Aa)) , -Ba .* reshape(Phi,1,1,[]) ) , numel(Ba),numel(L))
V = 2×2
-14.8148 -9.7561 -32.4316 -18.7207
  5 个评论
Bruno Luong
Bruno Luong 2022-7-17
@Paul But, I am surprised by this ...
It looks like the discrepency occurs when matrix size is between 2 and 9, beyond that pagemldivide seems to match backslash.
nmax = 100;
errx = zeros(1,nmax);
for n=1:nmax
A = rand(n,n,10);
B = rand(n,n,10);
X = pagemldivide(A,B);
errx(n) = norm(X(:,:,end)-A(:,:,end)\B(:,:,end));
end
plot(1:nmax, errx);
xlabel('Matrix size');
ylabel('Error betwen pagemldivide');
Paul
Paul 2022-7-18
编辑:Paul 2022-7-18
I as surprised by this too. Never would have occurred to me to test for different dimensions, I'm glad it occurred to you.
Further discussion here.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by