scottwr

Mr. Scott Wright

15 Reputation

One Badge

2 years, 160 days

MaplePrimes Activity


These are questions asked by scottwr

I have a list of values in the range from 1 to 6 inclusive:

    [> y := [ seq(rand(1..6)(), i = 1..10) ] : # step 1

I would like to replace all elements <= 3 with -1 and all elements > 3 with 1

    [> y := subs(1=-1, 2=-1, 3=-1, 4=1, 5=1, 6=1, y) : # step 2

Then calculate the cumulative sum

    [> y := [ seq(add(y[ .. i]), i = 1..n) ] : # step 3

And plot the results

    [> plot([ seq(i, i = 1..n) ], y) # step 4
 

This all works, but step 2 is downright ugly. Is there a more elegant expression available?

And step 4 requires I generate an extra data set for the x-axis. Is there a way to specify just the the y-values and have Maple assume the x-values are in the range  1..nops(y)? (Something akin to just  plot(y), which produces output I don't understand.)

By way of example, I am trying to replicate this MATLAB expression:

    > y = cumsum(2*(randi(6, 10, 1) <= 3) - 1); plot(y)

 

 

I have a simple nested for loop in a worksheet:

[> for x in [ -1, 1 ] do for y in [ -1, 1 ] do x*y end do end do

When I press Enter, nothing happens. What am I (as a novice) missing?

Scott

I have a solution to a physics free-fall problem:

times := -2.019619977*Unit('s'), 2.019619977*Unit('s')

I would like to extract the positive values.

After some tinkering, I came up with this solution:

sol:=[][]:
for i in times do:
    val := Split(i, output = coefficient):
    if 0 <= val then:
        sol := sol, val*Unit('s'):
    end if:
end do:
evalf(sol,3)

2.02 s

This works, but is inelegant and requires many expressions.

Is there a simpler way, possibly a single-line expression, to accomplish this?

Page 1 of 1