简体   繁体   English

简单的代码加速

[英]Simple Speed-up of code

It is known that MATLAB works slow with for loop. 众所周知,MATLAB在for循环中工作缓慢。 I have tried to vectorize the following code without success. 我试图将以下代码向量化,但没有成功。 Perhaps I am wrong with the implementation. 也许我的实现是错误的。

for I = NS2:-1:1
         A = 0;
         for J=1:8
            A = A + KS2(J,I)*FA(J);
         end
         S2 = S2 + ( SS2(1,I)*sin(A) + SS2(2,I)*cos(A) );
      end

where: FA = matrix 1x8 其中:FA =矩阵1x8

KS2 = matrix 8x25 KS2 =矩阵8x25

SS2 = matrix 2x25 SS2 =矩阵2x25

A = scalar A =标量

S2 = scalar S2 =标量

I try to improve it in this way: 我尝试以这种方式进行改进:

A = 0;
J = 1:8;
for I = NS2:-1:1

 A = FA(1,J)*KS2(J,I);

 S2 = S2 + ( SS2(1,I)*sin(A) + SS2(2,I)*cos(A) );
 end

However, the runtime for this improvement is similar to the original code. 但是,此改进的运行时类似于原始代码。

Try this instead (no loops): 尝试以下操作(无循环):

A = (FA*KS2).';  %'# A is now 25-by-1
S2 = SS2(1,:)*sin(A) + SS2(2,:)*cos(A);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM