日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Matlab计算速度优化(矩阵,bsxfun,repmat)

發布時間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Matlab计算速度优化(矩阵,bsxfun,repmat) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 前言
  • 正文
  • 測試

前言

最近再寫一個大的程序,需要考慮一下如何節約硬件資源,以及提高運算速度。

正文

bsxfun Binary Singleton Expansion Function
C = bsxfun(FUNC,A,B) applies the element-by-element binary operation
specified by the function handle FUNC to arrays A and B, with singleton
expansion enabled. FUNC can be one of the following built-in functions:

@plus Plus@minus Minus@times Array multiply@rdivide Right array divide@ldivide Left array divide@power Array power@max Binary maximum@min Binary minimum@rem Remainder after division@mod Modulus after division@atan2 Four-quadrant inverse tangent; result in radians@atan2d Four-quadrant inverse tangent; result in dgrees@hypot Square root of sum of squares@eq Equal@ne Not equal@lt Less than@le Less than or equal@gt Greater than@ge Greater than or equal@and Element-wise logical AND@or Element-wise logical OR@xor Logical EXCLUSIVE OR

測試

%這段測試了循環運算和矩陣運算耗時的差異,結果是矩陣快了僅10倍 testa=rand(1,100); testb=rand(100,100); tic result1=testa*testb; toc result2=zeros(1,100); tic for i=1:100result2(1,i)=testa*testb(:,i); end toc%這段測試的循環運算和bsx運算的差異,bsxfun快了30倍 A=rand(200,100); B=rand(200,100); beta=0.5; tic K1 = zeros(size(A,1),size(B,1)); for i = 1 : size(A,1)for j = 1 : size(B,1)K1(i,j) = exp(-sum((A(i,:)-B(j,:)).^2)/beta); end end toc tic sA = (sum(A.^2, 2)); sB = (sum(B.^2, 2)); K2 = exp(bsxfun(@minus,bsxfun(@minus,2*A*B', sA), sB')/beta); toc%這段測試的repmat擴容后矩陣運算和bsxfun運算的差異,bsxfun快了4倍A = magic(5); ticA = bsxfun(@minus, A, mean(A)); toc A=magic(5); tic A=A(min(A,repmat(mean(A),size(A,1),1))); toc

綜上,最好使用bsxfun解決矩陣運算的問題,其次是repmat擴容后使用矩陣運算,最次是使用循環運算。

總結

以上是生活随笔為你收集整理的Matlab计算速度优化(矩阵,bsxfun,repmat)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。