Quantcast

Re: Avoiding a loop

classic Classic list List threaded Threaded
2 messages Options
Frieder Nikolaisen Frieder Nikolaisen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Avoiding a loop

There was a piece of code missing:

P = [
1,      0; 
2,      50; 
5,      110;
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

 

Am 2017-05-10 17:23, schrieb Frieder Nikolaisen:

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

Re: Avoiding a loop

Le 10/05/2017 à 17:25, Frieder Nikolaisen a écrit :

There was a piece of code missing:

P = [
1,      0; 
2,      50; 
5,      110;
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

 

Am 2017-05-10 17:23, schrieb Frieder Nikolaisen:

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

It may be compacted with:
P = [   1.   2.      5.      10.    11.    15.
            0.   50.  110.   80.   200.   0. ]';
batt = 1000;
gen = 0;
terms = P(1:$-1,2) .* (P(2:$,1) - P(1:$-1,1));
for n = 1:5
    if P(n,2) <= 100 | batt > 800 then
        batt = batt - terms(n);
    else
        gen = gen + terms(n);
    end
mprintf("n = %d   batt = %d   gen = %d\n", n, batt, gen);
end
Then, as told by Tim, since the condition on batt may change batt according to n,
it is hard to go on without the loop.


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Loading...