If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby - Adding array indexes of one arryay with other one
9 posts
• Page 1 of 1
Ruby - Adding array indexes of one arryay with other one
Hi,
Wondered if there is any elegant Ruby shortcut to add float array indexes with other float array indexes while keeping their correct order.
For example, let's say we have this array:
1.5
2
3.3
6.4
And we want to add it this array:
1.5
2.3
1
2.4
To get this results at the output:
3
4.3
4.3
8.8
Possible?
Wondered if there is any elegant Ruby shortcut to add float array indexes with other float array indexes while keeping their correct order.
For example, let's say we have this array:
1.5
2
3.3
6.4
And we want to add it this array:
1.5
2.3
1
2.4
To get this results at the output:
3
4.3
4.3
8.8
Possible?
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
- adamszabo
- Posts: 667
- Joined: Sun Jul 11, 2010 7:21 am
Re: Ruby - Adding array indexes of one arryay with other one
Thank you Adam,
That's great solution when your variables are fixed and not changeable. Is there any way to do it without specifying the exact parameters? I mean just to tell Ruby to add together similar indexes of 2 given arrays.
That's great solution when your variables are fixed and not changeable. Is there any way to do it without specifying the exact parameters? I mean just to tell Ruby to add together similar indexes of 2 given arrays.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby - Adding array indexes of one arryay with other one
Well, I've reproduced it according to your example and it works
The exact code syntax is (assuming that the first array is @a, the second array is @b and the output is @c):
Thanks again!
The exact code syntax is (assuming that the first array is @a, the second array is @b and the output is @c):
- Code: Select all
@c=[@a,@b].transpose.map{|a| a.sum}
output 0, @c
Thanks again!
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby - Adding array indexes of one arryay with other one
Yes I forgot to mention that in my example I just put the same values that you did in your example, but you can put any values there, but I see you figured it out
But if you are just using a single ruby to send this out, you dont need the @c variable you can just write:
But if you are just using a single ruby to send this out, you dont need the @c variable you can just write:
- Code: Select all
output [@a,@b].transpose.map{|a| a.sum}
- adamszabo
- Posts: 667
- Joined: Sun Jul 11, 2010 7:21 am
Re: Ruby - Adding array indexes of one arryay with other one
adamszabo wrote:Yes I forgot to mention that in my example I just put the same values that you did in your example, but you can put any values there, but I see you figured it out
But if you are just using a single ruby to send this out, you dont need the @c variable you can just write:
- Code: Select all
output [@a,@b].transpose.map{|a| a.sum}
Yes, indeed
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
Re: Ruby - Adding array indexes of one arryay with other one
Also you are not limited to two arrays, as long as they all are the same size:
[[1.5, 2, 3.3, 6.4],[1.5, 2.3, 1, 2.4],[10,10,10,10]].transpose.map{|a| a.sum}
=> [13.0, 14.3, 14.3, 18.8]
[[1.5, 2, 3.3, 6.4],[1.5, 2.3, 1, 2.4],[10,10,10,10]].transpose.map{|a| a.sum}
=> [13.0, 14.3, 14.3, 18.8]
-
DaveyBoy - Posts: 131
- Joined: Wed May 11, 2016 9:18 pm
- Location: Leeds UK
Re: Ruby - Adding array indexes of one arryay with other one
Another way is zip() and reduce():
Works with any number of arrays as well:
This is what you HAVE to use in Flowstone 3, because it runs Ruby 1.9.3 and that Ruby version doesn't know sum() for arrays.
(Remember, FS 4 related stuff only in the slack group)
- Code: Select all
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
a.zip(b).map {|n| n.reduce(:+)}
Works with any number of arrays as well:
- Code: Select all
a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
c = [9, 10, 11, 12]
a.zip(b, c).map {|n| n.reduce(:+)}
This is what you HAVE to use in Flowstone 3, because it runs Ruby 1.9.3 and that Ruby version doesn't know sum() for arrays.
(Remember, FS 4 related stuff only in the slack group)
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby - Adding array indexes of one arryay with other one
Great stuff guys Thank you.
I'm collecting all this impish Ruby shortcuts and I'll probably post them as a Ruby expansion pack for newbies once the FS4 becomes official.
I'm collecting all this impish Ruby shortcuts and I'll probably post them as a Ruby expansion pack for newbies once the FS4 becomes official.
-
kortezzzz - Posts: 763
- Joined: Tue Mar 19, 2013 4:21 pm
9 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 75 guests