Page 1 of 1

Process an Array in Ruby

PostPosted: Tue Oct 18, 2016 8:26 am
by OllieMarshall92
Hi All,

Basically I want to find the sin([array]) (sine of each value in the array) and then export the array from ruby.

I can find the sine of a float but my code wont do the same to an array. Ruby comes up with an error something like "cant convert array to float".
I think I may need to iterate through the array, processing one at a time but I cant work out how to do that.

Ollie

Re: Process an Array in Ruby

PostPosted: Tue Oct 18, 2016 9:00 am
by tulamide
With "myarr" as the array that contains the data
Code: Select all
na = myarr.map { |v| Math.sin(v) }

Re: Process an Array in Ruby

PostPosted: Tue Oct 18, 2016 9:56 am
by Lex248
With RAD2DEG and RAD2GRAD module.

Re: Process an Array in Ruby

PostPosted: Tue Oct 18, 2016 10:26 am
by OllieMarshall92
Thanks, both of you.

My input is in degrees, how do I format it so if I put 180 in I get a 0 out? e.g. sin(180) = 0

Ollie

Re: Process an Array in Ruby

PostPosted: Tue Oct 18, 2016 12:42 pm
by Lex248
?