[Scilab-users] ugly code improvement (with vectorization)

classic Classic list List threaded Threaded
3 messages Options
Carrico, Paul Carrico, Paul
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Scilab-users] ugly code improvement (with vectorization)

Dear All,

 

Can I ask advice to correct this ugly/slow code (using vectorization)?

 

In the example here bellow, I want

-          to capture the non-null values of each line from the 5th column – each row can have different “length”

-          the 4th column indicates how many values there’re i.e the length

-          Then to put this values in a vector previously initialized

 

If I can extract the values with ‘’Elements(k,[5: Elements (k,4)]’’, I do not know how to put it the ‘‘View’’ matrix without using dynamic allocation ([View ; Elements (k,4) … indeed I’m using block and not explicit index

 

Thanks for your time

 

Paul

 

########################################################

View = zeros(dim,2); // dim previously calculated

 

n = 1;

for k = 1 : size(Elements,1)

    for m = 1 : Elements (k,4)

        View(n,1) = Elements (k,4+m);

        n = n+1;

    end

end

 

with

Elements = [

    1.     1.    29.    8.    81.    82.     83.     124.    165.    164.    163.    122.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.

    2.     1.    29.    8.    83.    84.     85.     126.    167.    166.    165.    124.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.

    3.     1.    29.    8.    85.    86.     87.     128.    169.    168.    167.    126.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.

    4.     1.    29.    8.    87.    88.     89.     130.    171.    170.    169.    128.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.

    5.     1.    29.    8.    89.    90.     91.     132.    173.    172.    171.    130.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.

….

    797.    1.    8.    20.    2193.    2194.    2195.    2219.    2257.    2256.    2255.    2218.    3515.    3516.    3517.    3541.    3579.    3578.    3577.      3540.    2854.    2856.    2918.    2916. 

    798.    1.    8.    20.    2195.    2196.    2197.    2220.    2259.    2258.    2257.    2219.    3517.    3518.    3519.    3542.    3581.    3580.    3579.      3541.    2856.    2858.    2920.    2918.

    799.    1.    8.    20.    2197.    2198.    2199.    2221.    2261.    2260.    2259.    2220.    3519.    3520.    3521.    3543.    3583.    3582.    3581.      3542.    2858.    2860.    2922.    2920. 

    800.    1.    8.    20.    2199.    2200.    2201.    2222.    2263.    2262.    2261.    2221.    3521.    3522.    3523.    3544.    3585.    3584.    3583.      3543.    2860.    2862.    2924.    2922.

    1047.    999.    3.    2.    3587.    3582.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0. 

    1048.    999.    3.    2.    3587.    3583.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.

    1049.    999.    3.    2.    3587.    3584.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.

    1050.    999.    3.    2.    3587.    3585.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.];

 

EXPORT CONTROL :
Cet email ne contient pas de données techniques
This email does not contain technical data

 


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Christophe Dang Ngoc Chan Christophe Dang Ngoc Chan
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: {EXT} ugly code improvement (with vectorization)

Hello Paul,

> De : Carrico, Paul
> Envoyé : mardi 27 juin 2017 09:06
>
> -  to capture the non-null values of each line from the 5th column - each row can have different "length"
> - the 4th column indicates how many values there're i.e the length
> - Then to put this values in a vector previously initialized
>  [
> Elements = [...]
>    1.     1.    29.    8.    81.    82.     83.     124.    165.    164.    163.    122.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.
[...]

I suggest:

1. Extract foo = Elements(:, 5:$)'

2. Use foo2 = foo(:) to have a vector.

3. Remove the zeros with something like index = (foo2 == 0) ; foo2(index) = []

Would this work?

--
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
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Carrico, Paul Carrico, Paul
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [EXTERNAL] Re: {EXT} ugly code improvement (with vectorization)

Hi Christophe,

Thanks for your suggestion - you were in the right way (good mind), I do not :-)

It works on the test here after, I just have to adapt it

Paul


####################################################################
mode(0)

Elements = [                          
1. 1. 29. 8. 81. 82. 83. 124. 165. 164. 163. 122. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
2. 1. 29. 8. 83. 84. 85. 126. 167. 166. 165. 124. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
3. 1. 29. 8. 85. 86. 87. 128. 169. 168. 167. 126. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
4. 1. 29. 8. 87. 88. 89. 130. 171. 170. 169. 128. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
5. 1. 29. 8. 89. 90. 91. 132. 173. 172. 171. 130. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
797. 1. 8. 20. 2193. 2194. 2195. 2219. 2257. 2256. 2255. 2218. 3515. 3516. 3517. 3541. 3579. 3578. 3577. 3540. 2854. 2856. 2918. 2916. 0 0 0 0 0.
798. 1. 8. 20. 2195. 2196. 2197. 2220. 2259. 2258. 2257. 2219. 3517. 3518. 3519. 3542. 3581. 3580. 3579. 3541. 2856. 2858. 2920. 2918. 0 0 0 0 0.
799. 1. 8. 20. 2197. 2198. 2199. 2221. 2261. 2260. 2259. 2220. 3519. 3520. 3521. 3543. 3583. 3582. 3581. 3542. 2858. 2860. 2922. 2920. 0 0 0 0 0.
800. 1. 8. 20. 2199. 2200. 2201. 2222. 2263. 2262. 2261. 2221. 3521. 3522. 3523. 3544. 3585. 3584. 3583. 3543. 2860. 2862. 2924. 2922. 0 0 0 0 0.
1047. 999. 3. 2. 3587. 3582. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
1048. 999. 3. 2. 3587. 3583. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
1049. 999. 3. 2. 3587. 3584. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
1050. 999. 3. 2. 3587. 3585. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.];

Elements2 = Elements;
Elements2(:,[1:4])=[];
n = size(Elements2,"*")
Elements2 = matrix(Elements2',n,1);
a = find(Elements2 == 0);
Elements2(a,1) = [];
Elements2

 

-----Message d'origine-----
De : users [mailto:[hidden email]] De la part de Dang Ngoc Chan, Christophe Envoyé : mardi 27 juin 2017 11:34 À : Users mailing list for Scilab Objet : [EXTERNAL] Re: [Scilab-users] {EXT} ugly code improvement (with vectorization)

Hello Paul,

> De : Carrico, Paul
> Envoyé : mardi 27 juin 2017 09:06
>
> -  to capture the non-null values of each line from the 5th column - each row can have different "length"
> - the 4th column indicates how many values there're i.e the length
> - Then to put this values in a vector previously initialized  [
> Elements = [...]
>    1.     1.    29.    8.    81.    82.     83.     124.    165.    164.    163.    122.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.     0.    0.    0.    0.
[...]

I suggest:

1. Extract foo = Elements(:, 5:$)'

2. Use foo2 = foo(:) to have a vector.

3. Remove the zeros with something like index = (foo2 == 0) ; foo2(index) = []

Would this work?

--
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
[hidden email]
https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.scilab.org_mailman_listinfo_users&d=DwIFAw&c=0hKVUfnuoBozYN8UvxPA-w&r=4TCz--8bXfJhZZvIxJAemAJyz7Vfx78XvgYu3LN7eLo&m=-jeS6eg34X1wOu7rjJUdp4_D6gD1unEepLJULOYTLo0&s=SdwDp7jNLsTyZClNObJsllYvRnaUOft504Nxbq4aSxo&e= 

EXPORT CONTROL :
Cet email ne contient pas de données techniques
This email does not contain technical data
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Loading...