Matrix Referencing

J

Jim Carter

I have a Matrix defined as follows:

wvp =
Matrix[[-40.0,0.006],[0.0,0.045],[32.0,0.18],[59.0,0.50],[70.0,0.74],
[100.0,1.93],[130.0,4.53]]

I want to interpolate within this 2D array, which I start to do with
the following equation:

m = (wvp[i,2]-wvp[i-1,2])/(wvp[i,1]-wvp[i-1,1])

This is readily done in C, but Ruby does not like the fact that I
subtract a one from the index i within the context of the array, i.e.,
[i-1,2]. Even if I try to fake it out by first declaring j = i - 1, and
using j in place of the i-1 within the array coordinate, I get the
following error:

C:\rails_projects\TestRubyConcepts\lib\main.rb:74:in
`humidity_correction': undefined method `-' for nil:NilClass
(NoMethodError)

It's claiming my method - is undefined. How do I defeat this?
 
N

Nathan Clark

I have a Matrix defined as follows:
=C2=A0wvp =3D
Matrix[[-40.0,0.006],[0.0,0.045],[32.0,0.18],[59.0,0.50],[70.0,0.74],
=C2=A0 =C2=A0 =C2=A0[100.0,1.93],[130.0,4.53]]

=C2=A0I want to interpolate within this 2D array, which I start to do wit= h
the following equation:

=C2=A0m =3D (wvp[i,2]-wvp[i-1,2])/(wvp[i,1]-wvp[i-1,1])

This is readily done in C, but Ruby does not like the fact that I
subtract a one from the index i within the context of the array, i.e.,
[i-1,2]. =C2=A0Even if I try to fake it out by first declaring j =3D i - = 1, and
using j in place of the i-1 within the array coordinate, I get the
following error:

C:\rails_projects\TestRubyConcepts\lib\main.rb:74:in
`humidity_correction': undefined method `-' for nil:NilClass
(NoMethodError)

It's claiming my method - is undefined. =C2=A0How do I defeat this?

"undefined method `-' for nil:NilClass" means that the operand to the
left of one of the '-' operators is Nil. That will most likely be one
of the wvp[...] accesses, assuming i and j are initialized correctly.

Check each of
wvp[i,2], wvp[i-1,2], wvp[i,1], and wvp[i-1,1] and see what they return.

Nathan
 
J

Jim Carter

Oops you are correct! MATLAB which I wrote the template code in has a
starting index 1, while Ruby has starting index 0. I overran my index.

I told myself in the beginning I had to be wary of this and I just fell
into the trap.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top