[Scilab-users] is vectorization possible

tim at wescottdesign.com tim at wescottdesign.com
Tue Sep 27 09:55:48 CEST 2016


OK.  I think I see where I went wrong.

There's a way to do this but it's a bit mind boggling.

If you can let ix be a rectangular matrix of indexes, then a(ix) will be 
a column vector of indexed values.  Then you can use 'matrix' to 
reassembly a(ix) into a rectangular matrix, the rows or columns of which 
you can sum up.  I don't know if it'll be faster than the for loop, 
though.

There may be an even better way of doing it, but I don't know what it 
is.

On 2016-09-27 00:28, paul.carrico at free.fr wrote:
> Thanks Tim for the answer, nevertheless it cannot work.
> 
> I want to make the sum of blocks of rows; here
> - 'i' is a vector
> - tmp is a vector of 20 rows in the current example
> - the loop does the job, but I do not understand why the vectorization
> fails ... is it a synthax error ?
> 
> Paul
> 
> pb: in my understanding, 'c' means 'column' and 'r' means row => for
> matrix operations
> 
> 
> 
> ----- Mail original -----
> De: "Tim Wescott" <tim at wescottdesign.com>
> À: users at lists.scilab.org
> Envoyé: Mardi 27 Septembre 2016 09:17:30
> Objet: Re: [Scilab-users] is vectorization possible
> 
> tmp = sum(a( [1 + (i-1)*n : i*n],:), 'c')
> 
> Or sum(..., 'r').  I can't remember which is which.  One makes a row,
> the other makes a column, but I can never remember if it's "sum all
> columns" or "sum into a column".
> 
> On Tue, 2016-09-27 at 09:08 +0200, paul.carrico at free.fr wrote:
>> Hi All
>> 
>> Is the vectorization possible for the example herebellow? everything I
>> tried failed !
>> 
>> Thanks for any help
>> 
>> Paul
>> 
>> ##################################################################
>> mode(0)
>> 
>> 
>> k = 100;
>> a = rand(k,1);
>> 
>> 
>> w = 5;
>> n = (k/w);
>> 
>> 
>> i = [1 : n]';
>> 
>> 
>> tmp = zeros(n,1);
>> 
>> 
>> // using vectorization
>> tmp(i,1) = sum(a( [1 + (i-1)*n : i*n],:)
>> abort
>> 
>> 
>> 
>> 
>> // same using a loop
>> for i = 1 : n
>>     tmp(i,1) = sum(a( [1 + (i-1)*w : i*w],:));
>> end
>> 
>> 
>> tmp
>> _______________________________________________
>> users mailing list
>> users at lists.scilab.org
>> http://lists.scilab.org/mailman/listinfo/users
> 
> --
> 
> 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