フィルターのクリア

How to vectorize A\B operation on slices of 3D matrices?

4 ビュー (過去 30 日間)
Jakub Tomasz Kaminski
Jakub Tomasz Kaminski 2021 年 3 月 18 日
編集済み: Bruno Luong 2021 年 4 月 7 日
Dear Matlab Users,
I am trying to solve a few separate sets of Ax = B equations with A\B or mldivide(A,B). A and B store equations components in their corresponding slices.
This is feasible with a for loop. Could it be done faster in some other way, with vectorization?
num_slices = 5;
A = rand(6,6, num_slices);
x = zeros(num_slices,6);
B = rand(6,1,num_slices);
% Working solution
for ii=1:num_slices
x(ii,:) =(A(:,:,ii)\B(:,:,ii))';
end
% Failed vectorization attempt
x_alternative =(A\B)'; % error here
The error I get is obvious as I do not use the "\" operation correctly:
Arguments must be 2-D, or at least one argument must be scalar. Use LDIVIDE (.\) for elementwise left division.
I would appreciate your help and suggestions in this matter.
EDIT:
I know about the option to flatten everything to sparse matices as suggested a few years ago here:
It would be great to learn if anything changed significantly from that time. Thank you!
Regards,
Jakub Kaminski
  2 件のコメント
Jan
Jan 2021 年 3 月 18 日
編集済み: Jan 2021 年 3 月 18 日
There is a tool in the file exchange which does exactly what you want in parallel. I searched for it for half an hour now, without success.
I've found it:
James Tursa
James Tursa 2021 年 4 月 7 日
編集済み: James Tursa 2021 年 4 月 7 日
The ndfun code used to be here, but the links are now broken:
But I did find the source code here:

サインインしてコメントする。

回答 (2 件)

Bruno Luong
Bruno Luong 2021 年 3 月 18 日
編集済み: Bruno Luong 2021 年 3 月 18 日
Yes there is new news since I post this MultipleQR fex (MEX required, OpenMP multithreading), for 6 x 6 this is the speed up on my LAPTOP, R2021a to solve one miillion linear systems A*x = y:
>> TestMultipleQRSolve
size(A) = [6 6 1000000]
size(y) = [6 1 1000000]
MultipleQRSolve time = 0.618821 [s]
Matlab loop time = 8.42263 [s]
For pure MATLAB sparse trick, you might be interested in my implementation of Multiple same size linear solver
  1 件のコメント
Bruno Luong
Bruno Luong 2021 年 3 月 21 日
編集済み: Bruno Luong 2021 年 3 月 21 日
I complete with the timings of the three methods
>> TestMultipleQRSolve
size(A) = [6 6 1000000]
size(y) = [6 1 1000000]
MultipleQRSolve time = 0.596974 [s]
Matlab loop time = 7.98282 [s]
SliceMultiSolver time = 2.56455 [s] % sparse trick

サインインしてコメントする。


Jan
Jan 2021 年 3 月 18 日
編集済み: James Tursa 2021 年 4 月 7 日
Do you have a compiler installed? Then use:
  1 件のコメント
Bruno Luong
Bruno Luong 2021 年 4 月 7 日
編集済み: Bruno Luong 2021 年 4 月 7 日
Benchmark with mmx('backslash', ...)
>> TestMultipleQRSolve
size(A) = [6 6 1000000]
size(y) = [6 1 1000000]
MultipleQRSolve time = 0.528698 [s]
Matlab loop time = 5.93315 [s]
SliceMultiSolver time = 1.8415 [s]
mmx(...) time = 0.369092 [s]

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by