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
comparing 2 arrays (ruby)
7 posts
• Page 1 of 1
comparing 2 arrays (ruby)
I need to do one of two things:
a) compare 2 arrays of elements and get an output with 0's (element at index not equal) and 1's (element at index equal)
or
b) determine which elements on input array changed after single retrig, and get an output like described above.
I guess the (a) is easier to implement.
It would be like this:
array1:
11
9
3
40
5
array2 (= array1 after change)
11
25
34
40
7
output:
1
0
0
1
0
(no sorting, no duplicate removing). How to make it with ruby?
a) compare 2 arrays of elements and get an output with 0's (element at index not equal) and 1's (element at index equal)
or
b) determine which elements on input array changed after single retrig, and get an output like described above.
I guess the (a) is easier to implement.
It would be like this:
array1:
11
9
3
40
5
array2 (= array1 after change)
11
25
34
40
7
output:
1
0
0
1
0
(no sorting, no duplicate removing). How to make it with ruby?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: comparing 2 arrays (ruby)
I think I found something, but... How to push these values to output?
I mean - ruby window shows correct answers, but when I add "output..." then it outputs only 0's or nothing.
I mean - ruby window shows correct answers, but when I add "output..." then it outputs only 0's or nothing.
- Attachments
-
- compare-ary.fsm
- (384 Bytes) Downloaded 793 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: comparing 2 arrays (ruby)
...not what I expected. Right now I can get this - works only with output changed to "string". But how to get simple array of 0/1 integers instead?
- Attachments
-
- compare-ary1.fsm
- (425 Bytes) Downloaded 790 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: comparing 2 arrays (ruby)
Now just simply convert them.
- Attachments
-
- compare-ary1.fsm
- (465 Bytes) Downloaded 800 times
-
TrojakEW - Posts: 111
- Joined: Sat Dec 25, 2010 10:12 am
- Location: Slovakia
Re: comparing 2 arrays (ruby)
it is possible with just one map command too
for comparing things with 2 options the ternary operator ( ? : ) always comes very handy
it works like this:
(condition) ? (when true) : (when false)
for comparing things with 2 options the ternary operator ( ? : ) always comes very handy
it works like this:
(condition) ? (when true) : (when false)
- Code: Select all
output @in1.map.with_index { |x,i| x = x==@in2[i] ? 1 : 0 }
-
Nubeat7 - Posts: 1347
- Joined: Sat Apr 14, 2012 9:59 am
- Location: Vienna
Re: comparing 2 arrays (ruby)
Thanks!
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
Re: comparing 2 arrays (ruby)
So this little schematic will detect which values in input array have changed (per cycle).
- Attachments
-
- detect-valchange.fsm
- (743 Bytes) Downloaded 800 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
Feel free to donate. Thank you for your contribution.
- tester
- Posts: 1786
- Joined: Wed Jan 18, 2012 10:52 pm
- Location: Poland, internet
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 70 guests