[Scilab-users] multiple element by element between large matrix and vector

Tim Wescott tim at wescottdesign.com
Sun Sep 27 22:44:51 CEST 2015


On Sun, 2015-09-27 at 03:53 -0700, David Chèze wrote:
> Hi all,
> 
> I was wondering what are the possibilities to do a product element by
> element between a matrix A (m rows ,n columns)and a vector (let say column
> vector v (m elements) ): I want each element of each column of A to be
> multiplied by each element of v at the same row. What I'm using for now is a
> product by a diagonal matrix based on v. For large matrix in data
> manipulation (typ. 100000 rows), it's necessary to use sparse to allocate
> better the diag matrix otherwise it's out of memory for usual machine.
> So i'm using --> diag(sparse(v)) * A
> 
> Have you best practice to share on this topic ?

You may already be using the best way.  Something to try would be

B = A;

for n = 1:size(B, 1)
  B(n, :) = v(n) * A(n, :);
end

I suspect it'll be slower, but you can always give it a whirl and see.
Use tic() and toc() for timing.

If you want to engage in heroic measures, write a C or FORTRAN function
to do what you want, and then learn how to dynamically link it to
Scilab.  I've done this in the past for quaternion arithmetic in a
Kalman filter, but it was a lot of work and not 100% robust in the face
of Scilab upgrades.

Also, if you don't already know about it, stacksize is a handy Scilab
function if you're working with large data arrays.  "stacksize max" will
either give you the biggest Scilab stack that can be had or crash your
machine, depending on your version (it appears to work in the current
version).  "stacksize(nnn)" will set your stacksize to nnn, without
crashing your machine (to my knowledge).  "stacksize" will report the
current stacksize.

-- 

Tim Wescott
www.wescottdesign.com
Control & Communications systems, circuit & software design.
Phone: 503.631.7815
Cell:  503.349.8432





More information about the users mailing list