fastest way to apply A\B on each matrix page

I would like to find an efficient fast way for calculating:
for i = 1:n
X(:,:,i) = A(:,:,i)\B(:,:,i)
end
where A and B are 10*10*n, and 10*1*n size matrices respectively. the matrices are large and must be called meny times. therefore I was thinking of replacing "for loops" with a faster way that does it very fast and not iteratively.

 Accepted Answer

Why insist on ARRAYFUN, your for-loop is perfectly fine. ARRAYFUN is a "vectoriztion" scam.
n = 100;
A = rand(10,10,n);
B = rand(10,1,n);
X = arrayfun(@(p) A(:,:,p)\B(:,:,p), 1:n, 'unif', 0);
X = cat(3,X{:});

5 Comments

speed is required for too many matrices like that. I don't know, I'm just looking for the fastest way. from what I know, in a loop, a procedure is done on each iteration, meaning that an iteration must be completed so that the next one is done. I'm not sure there is a way to do them all in once. Thank you for reply.
For that very reason that you just state why I call ARRAYFUN is a big scam.
User have impression thing is done once, no it does iteratively just like a FOR-LOOP and usually it slowser.
If you want do it "once" this FEX file do it really at once, not sure if it's fatre though due to overhead.
there is also another way to concatenate all matrices of each page diagonally and make one A\B, this way is really fast with sparse matrices, but this way creates a huge matrix and after completion I need to extract unknowns manually which I don't like this method.
Bruno Luong. sorry for misunderstanding. I did not mean to decieve. I only thought that arrayfun is a best replace for "loop". I'll edit the question.
I studied your MultiSolver. it was using concatenation diagonally and make a sparse matrix as I said. I see there's no better way. however ur using of repmat and rehsape was something speedy to extract unknowns without loops. I accept your answer. thanks, but I'd like to mention once more that it was misunderstanding. please don't use words like "big scam". thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!