Multiply large matrix by scalar - speed issue
Show older comments
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.
Accepted Answer
More Answers (2)
Matt Fig
on 18 Apr 2011
help mex
James Tursa
on 19 Apr 2011
0 votes
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 Comments
CP
on 19 Apr 2011
James Tursa
on 19 Apr 2011
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
on 19 Apr 2011
CP
on 19 Apr 2011
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!