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

CHEZE David 227480 david.cheze at cea.fr
Mon Sep 28 10:36:51 CEST 2015


Hi ,

Indeed it's the way I found to get it work on my machine.
Each element (double) of v is distinct of each other, it's a kind of sizing factor that I want to apply on all the columns of matrix A (double), according to index of the row.
Usually for such element by element I would have done:

B=A;
For k=1:size(v,1)
	B(:,k)=A(:,k) .* v
end
 but it's a loop that I want to remove on speeding-up purpose therefore I tried the proposed workaround.
 Thank you for your feedback.

David

-----Message d'origine-----
De : users [mailto:users-bounces at lists.scilab.org] De la part de Tim Wescott
Envoyé : dimanche 27 septembre 2015 22:45
À : Users mailing list for Scilab <users at lists.scilab.org>
Objet : Re: [Scilab-users] multiple element by element between large matrix and vector

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


_______________________________________________
users mailing list
users at lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


More information about the users mailing list