필터 지우기
필터 지우기

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 CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by