Re: [Scilab-users] automatic traverse question
as I did thank you -- View this message in context: http://mailinglists.scilab.org/automatic-traverse-question-tp4036327p4036383.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] automatic traverse question
Hello Erhy, >I studied your solution and think there will be a very huge array with >kronecker operator and generated the planes with a for loop. There is a common compromise between the used amount of memory and the algorithmic speed. There we are. >And now I have the problem with the max() function for all planes, >at which in the resulting plane each pixel should have the max. values of >the according pixels of the planes mentioned. > >max() works of a known number of planes: >resu=max(monos (:,:,1),monos (:,:,2),monos (:,:,3)) > >What is the smartest way to code, if the number of planes can differ? I am afraid that i don't clearly catch your point. If what i guess is not too far from what you mean, here is a way: MAX = monos(:,:,1); for i = 2:N MAX = max(MAX, monos(:,:,i)) end Samuel ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] linking (jumping) from line of code to an other
Jumps are generally bad style; I don't know if Scilab even supports them. If it does I advise you not to use them for something this trivial (error handling is the only place I've seen them seriously recommended, and even there they can be very problematical -- it's why people invented exceptions). For something that small the cost in making your code into spaghetti is larger than the cost of just doing it. The two ways that software professionals would deal with this are to either get rid of 100 as a "magic number" and assign its value to a variable (i.e., before the "for" statement assign PowerIncrement = 100), or rearrange the conditional so that the P = P + PowerIncrement line is only encountered once. Or do both. Certainly if you use the existing structure and if the two lines that increment P are supposed to do the same thing always, then using a named variable is a Good Thing -- that means that at some later date, when some poor ignorant fool reads your code they don't have to wonder if the "P+100" in one spot means the same thing as the "P+100" in another, AND they don't run the risk of changing the (hopefully proper) increment in one place but not another. And note, based on my experience writing software for the last 40 years, that often the poor ignorant fool who has to maintain your software is you, months or years down the road -- so it pays to make your code readable. Nothing makes you feel dumber than not being able to understand your own code, because no matter which way the blame flies, it always lands squarely on you. On Fri, 2017-05-12 at 11:15 +0200, Frieder Nikolaisen wrote: > Hello, > another question to solve the locomotive stuff, giving you a example > with the not implented code. > I don't want to code the same stuff twice. Thats why I want to jump > between lines of code. Is this possible and how? > P = 200; > DM = 1; > > for n = 1:10 > if DM == 1 then > if P > 100 then DM = 1 > P = P - 60 > disp('P bigger 100. DM = ' + string(DM)) > elseDM = 0 > P = P + 100 //instead of writing P = P + > 100, I would like ... > disp('P smaller 100. DM = ' + string(DM)) > end > else // D == 0 > if P > 100 then DM = 1 > disp('P bigger 100. DM = ' + > string(DM)) > P = P - 30 > elseDM = 0 // ... to continue here > disp('P smaller 100. DM = ' + > string(DM)) > P = P + 100 // to use this P = P + 100 > end > end > disp(string(n) + ' ' + string(P)) > end > > ___ > 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
Re: [Scilab-users] linking (jumping) from line of code to an other
The Solution is: Using whil: i = 1 a = 1 while i < 5 disp(i) a = a + 1 i = i +1 if a == 3 then i = i - 1 end end Am 2017-05-12 11:15, schrieb Frieder Nikolaisen: > Hello, > > another question to solve the locomotive stuff, giving you a example with the not implented code. > > I don't want to code the same stuff twice. Thats why I want to jump between lines of code. Is this possible and how? > > P = 200; > DM = 1; > > for n = 1:10 > if DM == 1 then > if P > 100 then DM = 1 > P = P - 60 > disp('P bigger 100. DM = ' + string(DM)) > else DM = 0 > P = P + 100 //instead of writing P = P + 100, I would like ... > disp('P smaller 100. DM = ' + string(DM)) > end > else // D == 0 > if P > 100 then DM = 1 > disp('P bigger 100. DM = ' + string(DM)) > P = P - 30 > else DM = 0 // ... to continue here > disp('P smaller 100. DM = ' + string(DM)) > P = P + 100 // to use this P = P + 100 > end > end > disp(string(n) + ' ' + string(P)) > end ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] automatic traverse question
I studied your solution and think there will be a very huge array with kronecker operator and generated the planes with a for loop. And now I have the problem with the max() function for all planes, at which in the resulting plane each pixel should have the max. values of the according pixels of the planes mentioned. max() works of a known number of planes: resu=max(monos (:,:,1),monos (:,:,2),monos (:,:,3)) What is the smartest way to code, if the number of planes can differ? -- View this message in context: http://mailinglists.scilab.org/automatic-traverse-question-tp4036327p4036377.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
[Scilab-users] linking (jumping) from line of code to an other
Hello, another question to solve the locomotive stuff, giving you a example with the not implented code. I don't want to code the same stuff twice. Thats why I want to jump between lines of code. Is this possible and how? P = 200; DM = 1; for n = 1:10 if DM == 1 then if P > 100 then DM = 1 P = P - 60 disp('P bigger 100. DM = ' + string(DM)) else DM = 0 P = P + 100 //instead of writing P = P + 100, I would like ... disp('P smaller 100. DM = ' + string(DM)) end else // D == 0 if P > 100 then DM = 1 disp('P bigger 100. DM = ' + string(DM)) P = P - 30 else DM = 0 // ... to continue here disp('P smaller 100. DM = ' + string(DM)) P = P + 100 // to use this P = P + 100 end end disp(string(n) + ' ' + string(P)) end ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Avoiding a loop
Hello Tim, Yes. batt is the is the State of Charge of the battery. P(n,2) (kN) is the power taken rom battery, engine or both. I will calculate the battery in As, so P(n,2) is only an in alternate value - but thats fare to complicated for the example code. "Proving that it's correct will be a pain." - prooving the for-loop and getting the same result with your way - that might be a prooving. As I have to code the loop anyway, I will consider your if the loop is very slow. Best regards Frieder Am 2017-05-11 18:48, schrieb Tim Wescott: > Depending on how often you switch between battery and generator, and > how icky-picky you're willing to be, there may be a way to reduce > computation. > > It looks like the term P(n,2) * (P(n+1,1) - P(n,1)) is always there, > and you're either adding it to 'gen' (is it energy production?) or > subtracting it from 'batt'. > > If you really want to go there, you can vectorize "if" statements by > using boolean expressions on vectors and the "find" function, which > returns indexes of true results. Then you can use "cumsum" on your > P(n,2) * (P(n+1,1) - P(n,1)) term to find where the battery state of > charge (I assume that's what 'batt' is) hits 800. > > It'll be complicated. It'll be prone to error. Proving that it's > correct will be a pain. But when you get it working, it'll be > considerably faster. > > On Thu, 2017-05-11 at 09:17 +0200, Frieder Nikolaisen wrote: > >> Thanks for all the answers. I feared that there is no way around a loop. During the process batt (Battery) is charged and discharged. In my example, it is only discharged. I will code the entire problem with a loop, maybe somebody knows something to speed up the process with the full problem. (Tim: I am not a programming pro, a C-function might not be a solution. ) Why do I try avoidng a loop? I do have txt-document with 50 000 to 100 000 lines about a (hybrid-)locomotive shunting process. I do need to optimize the energy managment. Because I am not mathemtic student, I have to solve the problem empirical (try and error). The programm has to run a few hundred times. With a matrix thats no problem, but with matrixes only, I can only calculate the diesel usage without any battery energy storage. Thanks for the checking my code anyway. Am 10.05.2017 um 20:53 schrieb Amanda Osvaldo: >> >>> What it's the equation you need to compute ? Perhaps I can help. I think it's possible to compute with something in this way: map = find (P(:,2) > 100 ); if batt > 800 then batt = batt - P(map,2) * (P(map+1,1) - P(map,1)); end On Wed, 2017-05-10 at 17:23 +0200, Frieder Nikolaisen wrote: >>> Hello, I did write an example code, but I do not like the time consuming way I solved the problem. With 50 000 lines in the matrix, it wouldn't be fun. How can I avoid using the for-loop? 10, 80; 11, 200 15, 0]; batt = 1000; gen = 0; n = 1 for n=1:5 if P(n,2) > 100 then if batt > 800 then batt = batt - P(n,2) * (P(n+1,1) - P(n,1)) else gen = gen + P(n,2) * (P(n+1,1) - P(n,1)) end else batt = batt - P(n,2) * (P(n+1,1) - P(n,1)) end disp('n ' + string(n)) disp('batt ' + string(batt)) disp('gen ' + string(gen)) end Thanks alot! Best regards Frieder ___ users mailing list users@lists.scilab.org [1] http://lists.scilab.org/mailman/listinfo/users [2] >>> ___ users mailing list users@lists.scilab.org [3] http://lists.scilab.org/mailman/listinfo/users [4] >> ___ users mailing list users@lists.scilab.org [5] http://lists.scilab.org/mailman/listinfo/users [6] Links: -- [1] mailto:users@lists.scilab.org [2] http://lists.scilab.org/mailman/listinfo/users [3] mailto:users@lists.scilab.org [4] http://lists.scilab.org/mailman/listinfo/users [5] mailto:users@lists.scilab.org [6] http://lists.scilab.org/mailman/listinfo/users ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users