# [Scilab-users] for-loop error Classic List Threaded 7 messages Open this post in threaded view
|

## [Scilab-users] for-loop error Hello all, I'm facing a trouble with a (in principle) very basic script... but I really don't understand the reason. Inside a bigger script, I have the following code: "Nentries=283; Nticks=20; Nstep=15; for j=1:Nticks     k=j*Nstep;     disp("j="+string(j)+"; Nstep="+string(Nstep)+"; k="+string(k)+" j*Nstep="+string(j*Nstep));     if(k<=Nentries) then         str1=strsplit(matrix(k,1),[11,20])(2);         str2 = strsubst(str1, "_","=");     else         str2="=";     end     tick_array_step_rot(j)="$\text {\rotatebox{90}{"+str2+"}}$" end" where "matrix" is a 283x5 string matrix. It usually works, but it sometimes gives me error of "invalid index" at the line of "str1". That's why I asked  to "disp" all the indexes and I find the following output:  j=1; Nstep=18; k=18 j*Nstep=18  j=2; Nstep=18; k=36 j*Nstep=36  j=3; Nstep=18; k=54 j*Nstep=54  j=4; Nstep=18; k=72 j*Nstep=72  j=5; Nstep=18; k=90 j*Nstep=90  j=6; Nstep=18; k=108 j*Nstep=108  j=7; Nstep=18; k=126 j*Nstep=126  j=8; Nstep=18; k=-112 j*Nstep=-112 If I change the matrix and the number of rows, I detect similar problems: at a certain point, the multiplication j*Nstep gets wrong. Do you have any suggestions? many thanks Anna -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: for-loop error Hi,   Are you sure that all the strings in matrix have at least 20 characters?   PS: Not related to the index issue but probably not a good idea to use a variable with same name as existing function matrix.   Regards, Rafael     -----Original Message----- From: users <[hidden email]> On Behalf Of anna28 Sent: Thursday, October 29, 2020 12:55 PM To: [hidden email] Subject: [Scilab-users] for-loop error   Hello all, I'm facing a trouble with a (in principle) very basic script... but I really don't understand the reason.   Inside a bigger script, I have the following code:   "Nentries=283; Nticks=20; Nstep=15; for j=1:Nticks     k=j*Nstep;     disp("j="+string(j)+"; Nstep="+string(Nstep)+"; k="+string(k)+" j*Nstep="+string(j*Nstep));     if(k<=Nentries) then         str1=strsplit(matrix(k,1),[11,20])(2);         str2 = strsubst(str1, "_","=");     else         str2="=";     end     tick_array_step_rot(j)="$\text {\rotatebox{90}{"+str2+"}}$" end"   where "matrix" is a 283x5 string matrix.   It usually works, but it sometimes gives me error of "invalid index" at the line of "str1". That's why I asked  to "disp" all the indexes and I find the following output:   j=1; Nstep=18; k=18 j*Nstep=18 j=2; Nstep=18; k=36 j*Nstep=36 j=3; Nstep=18; k=54 j*Nstep=54 j=4; Nstep=18; k=72 j*Nstep=72 j=5; Nstep=18; k=90 j*Nstep=90 j=6; Nstep=18; k=108 j*Nstep=108 j=7; Nstep=18; k=126 j*Nstep=126 j=8; Nstep=18; k=-112 j*Nstep=-112   If I change the matrix and the number of rows, I detect similar problems: at a certain point, the multiplication j*Nstep gets wrong.   Do you have any suggestions?   many thanks Anna     _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: for-loop error Hello, thanks for your answer. About the "PS", in fact, in my script, the name is not "matrix"... I've simplified the name to report it in the post (with a not 'clever' name, actually). About the answer itself, yes, all the elements have 20 characters. The point is the following: " j=7; Nstep=18; k=126 j*Nstep=126 j=8; Nstep=18; k=-112 j*Nstep=-112" Why does it make 8*18=-112? The matrix would have the index 8*18=144 but clearly it doesn't the index -112.... -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: for-loop error That is weird. Your code in my Scilab 6.1 on Win 10, shows:   "j=7; Nstep=18; k=126 j*Nstep=126"   "j=8; Nstep=18; k=144 j*Nstep=144" Regards, Rafael -----Original Message----- From: users <[hidden email]> On Behalf Of anna28 Sent: Thursday, October 29, 2020 4:08 PM To: [hidden email] Subject: Re: [Scilab-users] for-loop error Hello, thanks for your answer. About the "PS", in fact, in my script, the name is not "matrix"... I've simplified the name to report it in the post (with a not 'clever' name, actually). About the answer itself, yes, all the elements have 20 characters. The point is the following: " j=7; Nstep=18; k=126 j*Nstep=126 j=8; Nstep=18; k=-112 j*Nstep=-112" Why does it make 8*18=-112? The matrix would have the index 8*18=144 but clearly it doesn't the index -112.... -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: for-loop error In reply to this post by anna28 I've disabled everything inside the loop, in order to understand how the "i*Nstep" goes on. That's the result: i=1; Nstep=13; k=13 i*Nstep=13  i=2; Nstep=13; k=26 i*Nstep=26  i=3; Nstep=13; k=39 i*Nstep=39  i=4; Nstep=13; k=52 i*Nstep=52  i=5; Nstep=13; k=65 i*Nstep=65  i=6; Nstep=13; k=78 i*Nstep=78  i=7; Nstep=13; k=91 i*Nstep=91  i=8; Nstep=13; k=104 i*Nstep=104  i=9; Nstep=13; k=117 i*Nstep=117  i=10; Nstep=13; k=-126 i*Nstep=-126  i=11; Nstep=13; k=-113 i*Nstep=-113  i=12; Nstep=13; k=-100 i*Nstep=-100  i=13; Nstep=13; k=-87 i*Nstep=-87  i=14; Nstep=13; k=-74 i*Nstep=-74  i=15; Nstep=13; k=-61 i*Nstep=-61  i=16; Nstep=13; k=-48 i*Nstep=-48  i=17; Nstep=13; k=-35 i*Nstep=-35  i=18; Nstep=13; k=-22 i*Nstep=-22  i=19; Nstep=13; k=-9 i*Nstep=-9  i=20; Nstep=13; k=4 i*Nstep=4 does anybody have an idea??? thanks! anna -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users In reply to this post by anna28 sorry to everybody, I understood my fault: I defined Nstep=int8 and that was avoiding number larger than 127... ...I was sure it should do a naif mistake. cheers Anna -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users In reply to this post by anna28 --> int8(8)*18  ans  =  -112 -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users