使用向量化求解多个线性系统 [英] Solving multiple linear systems using vectorization
问题描述
很抱歉,如果这很明显,但我搜索了一会儿却没有找到任何东西(或错过了它).
Sorry if this is obvious but I searched a while and did not find anything (or missed it).
我正在尝试求解形式为 Ax = B 的线性系统,其中 A 是一个4x4矩阵,而 B 是一个4x1向量.
I'm trying to solve linear systems of the form Ax=B with A a 4x4 matrix, and B a 4x1 vector.
我知道对于单个系统,我可以使用mldivide
来获取 x :x=A\B
.
I know that for a single system I can use mldivide
to obtain x: x=A\B
.
但是我试图解决大量系统(可能> 10000),并且我不愿意使用for循环,因为在许多MATLAB问题中,我被告知它比矩阵公式要慢得多.
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.
然后我的问题是:有没有一种方法可以使用向量化将 A 4x4x N 和 B 矩阵4x N ?
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:我不知道它是否重要,但是 B 向量对于所有系统都是相同的.
PS: I do not know if it is important but the B vector is the same for all the systems.
推荐答案
您应该使用for循环.如果A
保持不变并且B
更改,则预先计算因式分解并重新使用它可能会有好处.但是对于您的问题,其中A
发生变化,而B
保持不变,除了解决N个线性系统外,别无选择.
You should use a for loop. There might be a benefit in precomputing a factorization and reusing it, if A
stays the same and B
changes. But for your problem where A
changes and B
stays 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.
这篇关于使用向量化求解多个线性系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!