Re: [Scilab-users] is vectorization possible
Le 27/09/2016 à 09:08, paul.carr...@free.fr a écrit : Hi All Is the vectorization possible for the example herebellow? everything I tried failed ! if a is a vector, it is quite straight forward: sum(matrix(a,w,-1),1).' k=100;a=rand(k,1);w=5;n=k/w; tmp = zeros(n,1); for i = 1 : n tmp(i,1) = sum(a( [1 + (i-1)*w : i*w],:)); end tmp-sum(sum(matrix(a,w,n,-1),3),1).' If a is matrix it is more tricky: sum(sum(matrix(a,w,n,-1),3),1).' k=100;a=rand(k,4);w=5;n=k/w; tmp = zeros(n,1); for i = 1 : n tmp(i,1) = sum(a( [1 + (i-1)*w : i*w],:)); end tmp-sum(sum(matrix(a,w,n,-1),3),1).' Serge 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@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
Hello, > De : paul.carr...@free.fr > Envoyé : mardi 27 septembre 2016 09:08 > > for i = 1 : n > tmp(i,1) = sum(a( [1 + (i-1)*w : i*w],:)); > end So you want to sum on a window which width is w, isn't it? Not full vectorization, but you might loop on w (=5) instead of n (=20), something like for i = 1:w tmp = tmp + a(i : n-w-1 + i) end It might be faster. -- Christophe Dang Ngoc Chan Mechanical calculation engineer This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
thanks to both of you I works now Paul ### mode(0) k = 1E6; a = rand(k,1); w = 2; n = (k/w); // same using a loop tic() tmp2 = zeros(n,1); for i = 1 : n tmp2(i,1) = sum(a([1 + (i-1)*w : i*w],:)); end duree_loop = toc() // using vectorization tic() tmp = zeros(n,1); tmp = sum(matrix(a,w,n),'r')'; duree_vectorization = toc() max_error = max(abs(tmp - tmp2)) - Mail original - De: "ol.bond" <oleksiy.b...@gmail.com> À: users@lists.scilab.org Envoyé: Mardi 27 Septembre 2016 09:59:12 Objet: Re: [Scilab-users] is vectorization possible As just mentioned by Tim, you have to transform your vector 'a' to a matrix, and then apply summation: tmp = sum(matrix(a,w,n)','c'); 2016-09-27 16:56 GMT+09:00 Tim Wescott [via Scilab / Xcos - Mailing Lists Archives] < [hidden email] > : 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, [hidden email] 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" < [hidden email] > > À: [hidden email] > 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, [hidden email] 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 >> [hidden email] >> http://lists.scilab.org/mailman/listinfo/users > > -- > > Tim Wescott > www.wescottdesign.com > Control & Communications systems, circuit & software design. > Phone: target="_blank">503.631.7815 > Cell: target="_blank">503.349.8432 > > > ___ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users > ___ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users If you reply to this email, your message will be added to the discussion below: http://mailinglists.scilab.org/Scilab-users-is-vectorization-possible-tp4034639p4034644.html To start a new topic under Scilab users - Mailing Lists Archives, email [hidden email] To unsubscribe from Scilab users - Mailing Lists Archives, click here . NAML View this message in context: Re: is vectorization possible Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
As just mentioned by Tim, you have to transform your vector 'a' to a matrix, and then apply summation: tmp = sum(matrix(a,w,n)','c'); 2016-09-27 16:56 GMT+09:00 Tim Wescott [via Scilab / Xcos - Mailing Lists Archives] <ml-node+s994242n4034644...@n3.nabble.com>: > 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, [hidden email] > <http:///user/SendEmail.jtp?type=node=4034644=0> 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" <[hidden email] > <http:///user/SendEmail.jtp?type=node=4034644=1>> > > À: [hidden email] > <http:///user/SendEmail.jtp?type=node=4034644=2> > > 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, [hidden email] > <http:///user/SendEmail.jtp?type=node=4034644=3> 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 > >> [hidden email] <http:///user/SendEmail.jtp?type=node=4034644=4> > >> 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 > > [hidden email] <http:///user/SendEmail.jtp?type=node=4034644=5> > > http://lists.scilab.org/mailman/listinfo/users > > ___ > users mailing list > [hidden email] <http:///user/SendEmail.jtp?type=node=4034644=6> > http://lists.scilab.org/mailman/listinfo/users > > > -- > If you reply to this email, your message will be added to the discussion > below: > http://mailinglists.scilab.org/Scilab-users-is-vectorization-possible- > tp4034639p4034644.html > To start a new topic under Scilab users - Mailing Lists Archives, email > ml-node+s994242n2602246...@n3.nabble.com > To unsubscribe from Scilab users - Mailing Lists Archives, click here > <http://mailinglists.scilab.org/template/NamlServlet.jtp?macro=unsubscribe_by_code=2602246=b2xla3NpeS5ib25kQGdtYWlsLmNvbXwyNjAyMjQ2fDE2MDI0ODA1Mzg=> > . > NAML > <http://mailinglists.scilab.org/template/NamlServlet.jtp?macro=macro_viewer=instant_html%21nabble%3Aemail.naml=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> > -- View this message in context: http://mailinglists.scilab.org/Scilab-users-is-vectorization-possible-tp4034639p4034646.html Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
On 2016-09-27 00:48, Samuel Gougeon wrote: > Le 27/09/2016 09:17, Tim Wescott a écrit : > >> .../... >> >> 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". > :) > Never did i, until i realized that operating ACROSS Rows yields a Row, or > across Columns Yields a Column. > Nevertheless i vaguely remember having met very few functions for which it is > the opposite (but for good understandable reasons). I just make a matrix 'thing', then do size(sum(thing, 'c')). Then I know. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
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.carr...@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" <t...@wescottdesign.com> À: users@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.carr...@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@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@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
Le 27/09/2016 09:17, Tim Wescott a écrit : .../... 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". :) Never did i, until i realized that operating *across* Rows yields a Row, or across Columns Yields a Column. Nevertheless i vaguely remember having met very few functions for which it is the opposite (but for good understandable reasons). Samuel ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] is vectorization possible
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" <t...@wescottdesign.com> À: users@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.carr...@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@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@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users