# Simple question on rbgsl vectors

**URL:** <https://www.ruby-forum.com/t/simple-question-on-rbgsl-vectors/108538>\
**Category:** Ruby\
**Created:** [July 4, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/simple-question-on-rbgsl-vectors/108538 "2007-07-04T14:51:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [July 4, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/simple-question-on-rbgsl-vectors/108538/1 "2007-07-04T14:51:04Z")

</div>

Hi,

I’m trying to do a simple thing : access elements of a vector  
specified by their index. In Matlab/Octave i would do,

newVector=oldVector( [1, 3, 56, 72] )

How does this work with Ruby vectors?

oldVector.get(3,4,5) returns the first 3 elements, where I would  
expect the 2nd, 3d and 4th.

I’ve had to do a twisted version of this using arrays and lists,

a2=[]  
(0…200).step(10) {|i| a2 \<\< a[i] }

I find it very inefficient and annoying, plus I do need to use  
vectors rather than arrays and lists and do not really know how to  
convert between these three ☹

Oh, and just a quick question: how can i do two instructions in one  
block ? I have to repeat the same instructions over and over like  
shown below…

a2=[]  
b2=[]  
(0…200).step(10) {|i| a2 \<\< a[i] }  
(0…200).step(10) {|i| b2 \<\< b[i] }

Best regards,

baptiste

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [July 4, 2007, 4:08pm UTC](https://www.ruby-forum.com/t/simple-question-on-rbgsl-vectors/108538/2 "2007-07-04T16:08:14Z")

</div>

Dear Baptiste,

you can get reference for the gsl bindings for ruby  
here:

[http://rb-gsl.rubyforge.org/ref.html](http://rb-gsl.rubyforge.org/ref.html) .

> oldVector.get(3,4,5) returns the first 3 elements, where I would  
> expect the 2nd, 3d and 4th.

oldVector.get([3,4,5]) worked for me - I’m not quite sure  
why this:

> oldVector.get(3,4,5) returns the first 3 elements

happens…

> I’ve had to do a twisted version of this using arrays and lists,
> 
> a2=[]  
> (0…200).step(10) {|i| a2 \<\< a[i] }
> 
> I find it very inefficient and annoying, plus I do need to use  
> vectors rather than arrays and lists and do not really know how to  
> convert between these three ☹

You’ll find info about that on the website given above.

> Oh, and just a quick question: how can i do two instructions in one  
> block ? I have to repeat the same instructions over and over like  
> shown below…

You can use the same index several times:

(0…200).step(10) {|i| a2 \<\< a[i] ; b2\<\<b[i] }

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [July 6, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/simple-question-on-rbgsl-vectors/108538/3 "2007-07-06T21:58:34Z")

</div>

Thanks for the reply, although I’m very confused now.

Does anybody see this odd behavior:

irb(main):001:0\> require(“rbgsl”)  
=\> true  
irb(main):002:0\> v = GSL::Vector[0…5]  
=\> GSL::Vector: [ 0.000e+00 1.000e+00 2.000e+00 3.000e+00 4.000e+00  
5.000e+00 ]  
irb(main):003:0\> v[2]  
=\> 2.0  
irb(main):004:0\> v[1, 3, 4]  
=\> GSL::Vector: [0.000e+00 1.000e+00 2.000e+00]

\*\*\*\* where is should expect \*\*\*\*

=\> GSL::Vector: [1.000e+00 3.000e+00 4.000e+00]

This is most annoying as I cannot do anything useful with such behavior.

irb 0.9.5 (05/04/13)  
ruby 1.8.6 (2007-03-13 patch level 0) [i386 linux]

Thanks,

baptiste
