Multiply large matrix by scalar - speed issue
古いコメントを表示
Ok this probably doesn't have an answer, but I have a large-ish matrix (1024x1024) which I want to scale by a constant. I need to do this for two equal sized matrices. Unfortunately, when I do this using Matrix = Matrix*c and Matrix2 = Matrix2*c2, it takes 90% of the time of the entire rest of the code, (thereby slowing things to a crawl) which does several numeric integrations and calculations on vectors of size 1024 and the like. I was wondering if there's some other way to get this to go faster, though it seems doutful since the entirety of the code in question is Matrix=Matrix*c. Well any help is appreciated.
採用された回答
その他の回答 (2 件)
Matt Fig
2011 年 4 月 18 日
help mex
James Tursa
2011 年 4 月 19 日
0 投票
MATLABs matrix * scalar is pretty fast and multi-threaded to boot. Unless you have complex numbers involved I don't think you will be able to speed this up, even with a mex routine. It is possible that Matrix and Matrix2 are shared and that is why the scalar multiplications are taking so long because it involves a data-copy and can't be done in-place. Can't tell without seeing more of your code.
4 件のコメント
CP
2011 年 4 月 19 日
James Tursa
2011 年 4 月 19 日
What size is W_array? Every time you do an array slice in MATLAB an entire data set copy takes place. i.e., the simple expression W_array(1:1024,1:1024) is an array slice where 1024*1024 data elements must first be copied to a new memory location, then the data at this new location gets multiplied by 1/tau, then that result gets copied back into W_array(1:1024,1:1024). In all likelihood it is the data copies that are using the most time, not the multiplies themselves. In that case a mex routine *can* avoid the data copy and run faster. Which gets me back to my first question: What size are your arrays?
CP
2011 年 4 月 19 日
CP
2011 年 4 月 19 日
カテゴリ
ヘルプ センター および File Exchange で Write C Functions Callable from MATLAB (MEX Files) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!