Sorry if this is obvious but I searched a while and did not find anything (or missed it).
I’m trying to solve linear systems of the form Ax=B with A a 4×4 matrix, and B a 4×1 vector.
I know that for a single system I can use mldivide to obtain x: x=A\B.
However I am trying to solve a great number of systems (possibly > 10000) and I am reluctant to use a for loop because I was told it is notably slower than matrix formulation in many MATLAB problems.
My question is then: is there a way to solve Ax=B using vectorization with A 4x4x N and B a matrix 4x N ?
PS: I do not know if it is important but the B vector is the same for all the systems.
You should use a for loop. There might be a benefit in precomputing a factorization and reusing it, if
Astays the same andBchanges. But for your problem whereAchanges andBstays the same, there’s no alternative to solving N linear systems.You shouldn’t worry too much about the performance cost of loops either: the MATLAB JIT compiler means that loops can often be just as fast on recent versions of MATLAB.