Discussion:
Negative array indices
(too old to reply)
Jeff
2006-01-05 17:31:09 UTC
Permalink
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
PB
2006-01-05 18:12:53 UTC
Permalink
Post by Jeff
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
Maybe, if you tell us how Fortran handles negative indices.

/PB
Christopher Hulbert
2006-01-05 18:10:39 UTC
Permalink
Post by PB
Post by Jeff
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
Maybe, if you tell us how Fortran handles negative indices.
/PB
Fortran allows you to set the lower bound (i.e. starting index).

so:
double precision,dimension(-1:9,10) :: a

is a 10x10 matrix.


To solve the problem,
Just add the lower index of the declaration to you matrix
John D'Errico
2006-01-05 18:15:52 UTC
Permalink
Post by Jeff
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?objectId=1168&objectType=file

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
PB
2006-01-05 18:32:41 UTC
Permalink
Post by Jeff
Post by Jeff
I'm in the process of converting a fairly large fortran model
to
Post by Jeff
matlab.
Post by Jeff
However, I'm having a problem with array indices. Fortran
allows
Post by Jeff
negative
Post by Jeff
indices ie. x(-1,10) whereas matlab does not.
Anyone have any ideas hot to overcome such a problem?
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=1168&objectType=file>
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
I´ve never even thought of this. As matlab is the first and only
language that I can use for something real, I´ve never even thoughth
of this. I did , some years ago , take a course in Java, but it
seemed to slow for engineering pusrposes so i didn´t bother learning
it. Maybe I was wrong? Still, for every serious calculation I do, I
either use Matlab or pen and paper.

/PB
kia
2006-01-05 21:01:42 UTC
Permalink
Post by Jeff
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.
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=1168&objectType=file

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..."
John D'Errico
2006-01-05 21:28:58 UTC
Permalink
Post by Jeff
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=1168&objectType=file>
"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..."
So? It admits to the fact. Would you rather it
claimed to be a professional solution when it
was not? What is your point?

Feel free to write your own if this is inadequate
for your purposes. And if you can do better, then
post it yourself, reaping the praise of millions.
If not, then apparently you have added nothing of
value here.

John
John
2006-01-05 22:51:40 UTC
Permalink
Post by John D'Errico
Post by Jeff
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=1168&objectType=file>
"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..."
So? It admits to the fact. Would you rather it
claimed to be a professional solution when it
was not? What is your point?
Feel free to write your own if this is inadequate
for your purposes. And if you can do better, then
post it yourself, reaping the praise of millions.
If not, then apparently you have added nothing of
value here.
John
Steady John

Text is quote from Nabeel Azar's description of his varbase package.

Poster should have Ack that fact.

Keep up the good work

cheers
john
John D'Errico
2006-01-05 23:36:43 UTC
Permalink
Post by John
Steady John
Yes. I know that was the description by Nabeel. When
I get irritated by something, I'll write the response
I really want to write, then delete it before posting.
This time I posted it before deleting. 8-)

My apologies.

John
--
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
boe
2006-01-05 19:01:48 UTC
Permalink
Post by Jeff
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
Ken Plotkin
2006-01-06 02:53:16 UTC
Permalink
Post by Jeff
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

Continue reading on narkive:
Loading...