Negative array indices 
Author Message
 Negative array indices

Hi,

I'm in the process of converting a fairly large fortran model to matlab.

However, I'm having a problem with array indices.  Fortran allows negative
indices ie. x(-1,10) whereas matlab does not.

Anyone have any ideas hot to overcome such a problem?

Any help is appreciated!
Jeff



Tue, 24 Jun 2008 01:31:09 GMT  
 Negative array indices

Quote:

> Hi,

> I'm in the process of converting a fairly large fortran model to matlab.

> However, I'm having a problem with array indices.  Fortran allows negative
> indices ie. x(-1,10) whereas matlab does not.

> Anyone have any ideas hot to overcome such a problem?

Well, the first question that pops into my mind (particularly since you use the adjective
"large") are things like: why are you converting this code?  More information is required
to give you a truly useful answer (opposed to just addressing the negative indices issue).
Why not compile the Fortran code into a library and call it from matlab?

cheers,

paulv

p.s. x-post to matlab ng removed.

--
Paul van Delst



Tue, 24 Jun 2008 02:05:45 GMT  
 Negative array indices

Quote:

> I'm in the process of converting a fairly large fortran model to matlab.

> However, I'm having a problem with array indices.  Fortran allows negative
> indices ie. x(-1,10) whereas matlab does not.

> Anyone have any ideas hot to overcome such a problem?

An option:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objec...

HTH,
John D'Errico

--
The best material model of a cat is another, or preferably the same, cat.
A. Rosenblueth, Philosophy of Science, 1945

Those who can't laugh at themselves leave the job to others.
Anonymous



Tue, 24 Jun 2008 02:15:52 GMT  
 Negative array indices

Quote:

> Hi,

> I'm in the process of converting a fairly large fortran model to matlab.

> However, I'm having a problem with array indices.  Fortran allows negative
> indices ie. x(-1,10) whereas matlab does not.

> Anyone have any ideas hot to overcome such a problem?

> Any help is appreciated!
> Jeff

some options to consider

1) You could of course just shift the array, but I don't think that is
wat you want.

2) What may be easier is to double your array indices for positive
values and -double+1 them for the negative ones. This means
-2 --> 5
-1 --> 3
0 --> 1
1 --> 2
2 --> 4

3) you could also keep two separate vectors, one containing values
corresponding to thos that had negative indices and one for those that
had positive indices

4) on filling the array you could keep a vector with indices as well.
every time you add a value to your vector (x), you add it's index to
another vector (idx). If you then need to retrieve a value, use
something like
retrievedValue=x(find(idx==neededIndex))

Hope these ideas help you to come up with a practical solution,

good luck

Jorg Entzinger



Tue, 24 Jun 2008 03:01:48 GMT  
 Negative array indices
Quote:

> > I'm in the process of converting a fairly large fortran model to matlab.
> > However, I'm having a problem with array indices.  Fortran allows negative
> > indices ie. x(-1,10) whereas matlab does not.

> An option:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objec...

A self disqualitying one at that:

"The varbase package allows you to create objects with the base index of
your choice...This is certainly not a professional grade production.
It's a "bare bones" system, lacking what many will consider critical
features..."



Tue, 24 Jun 2008 05:01:42 GMT  
 Negative array indices

Quote:

>Hi,

>I'm in the process of converting a fairly large fortran model to matlab.

>However, I'm having a problem with array indices.  Fortran allows negative
>indices ie. x(-1,10) whereas matlab does not.

>Anyone have any ideas hot to overcome such a problem?

>Any help is appreciated!
>Jeff

Does Matlab have something equivlent to Fortran's EQUIVALENCE?

Back before Fortran allowed negative indices, I used to overlap arrays
to allow that:

      dimension a(10), adum(11)
      equivalence (adum(11),a)

let me use a from -10 to 10.

Or, why not just add an offset to the index?  If you want i to go to
-10, reference the array with i+10.

Ken Plotkin



Tue, 24 Jun 2008 10:53:16 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. negative array index

2. RubyChangeRequest #U002: new proper nameforHash#indexes, Array#indexes

3. RubyChangeRequest #U002: new proper namefor Hash#indexes, Array#indexes

4. question about negative indices in fortran 77

5. question about negative indices in fortran 77

6. Built-in func to calculate negative indices??

7. finding 1d index in 2d array of indices

8. Q : legacy of negative index

9. Index Values of an Array inside a Cluster inside an Array

10. array as index to array

11. how to access arrays with variable for array names and index

12. automatic arrays with negative size in g95 and gfortran

 

 
Powered by phpBB® Forum Software