Indexing "1-vectors"

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Indexing "1-vectors"

Ted.Harding
Apologies if this has all been settled before.

Using Octave-1.0, I have written a function which works fine with
vector arguments. It includes lines like

        rhod( rhod<=-1 ) = 0.5*( rho0( rhod<=-1 ) - 1);

to modify those elements of a vector satisfying a condition,
namely " rhod(i) <= -1 ", with reference to another vector rho0.

So far so good, provided the variables are vectors with more than
one element. But I would also like this function to work if the
arguments (and therefore the variables like "rhod", "rho0") are 1x1
matrices (equivalent to scalars). However, in such a case when the
condtion is false, Octave says

        error: index invalid or out of range for scalar type

and there seems to be no way to prevent this. Yet, if Octave could
be forced to view the "scalar" as a vector of length 1 rather than as a scalar
proper, the indexing behaviour should also work in this case, according
to the rule which seems to apply for M-vectors, namely:-

        For M-vectors A, B, C
        A( boolean_fn(C) ) = B( boolean_fn(C) )

should carry out the assignment A(i)=B(i) for elements (i)
for which boolean_fn(C(i)) is true, and do nothing for all other (i).
This works even if there are no elements (i) for which the condition
is true.

The dimension does not have to be greater than one for this rule to
be workable. Is it possible to force this behaviour in Octave-1.0,
or do I have to write a special-case "if ... " section to cope with
the scalar case?

With thanks,
Ted.                                     ([hidden email])

Reply | Threaded
Open this post in threaded view
|

Re: Indexing "1-vectors"

John Eaton-4
I believe that I have fixed this for 1.1.1, which should be available
within a few weeks to fix bugs found in 1.1.0.

Given a scalar variable S, Octave now allows assignments like

  S (0)      = []  ==>   S
  S (0, 0)   = []  ==>   S
  S ([])     = []  ==>   S
  S ([], []) = []  ==>   S

Other combinations of 0 and [] indices are also no-ops, and should
all work independent of the value of do_fortran_indexing.

This does not cover all the cases that Matlab handles, but I think
some of those are bugs.  For example, Matlab allows

  S (:, [])  = []  ==>  S

but

  S (1, [])  = []  ==>  error

That would seem to be inconsistent, since for a scalar, a colon would
normally be the same as an index of 1.

: Apologies if this has all been settled before.
:
: Using Octave-1.0, I have written a function which works fine with
: vector arguments. It includes lines like
:
:         rhod( rhod<=-1 ) = 0.5*( rho0( rhod<=-1 ) - 1);
:
: to modify those elements of a vector satisfying a condition,
: namely " rhod(i) <= -1 ", with reference to another vector rho0.
:
: So far so good, provided the variables are vectors with more than
: one element. But I would also like this function to work if the
: arguments (and therefore the variables like "rhod", "rho0") are 1x1
: matrices (equivalent to scalars). However, in such a case when the
: condtion is false, Octave says
:
:         error: index invalid or out of range for scalar type
:
: and there seems to be no way to prevent this. Yet, if Octave could
: be forced to view the "scalar" as a vector of length 1 rather than
: as a scalar proper, the indexing behaviour should also work in this
: case, according to the rule which seems to apply for M-vectors, namely:-
:
:         For M-vectors A, B, C
:         A( boolean_fn(C) ) = B( boolean_fn(C) )
:
: should carry out the assignment A(i)=B(i) for elements (i)
: for which boolean_fn(C(i)) is true, and do nothing for all other (i).
: This works even if there are no elements (i) for which the condition
: is true.
:
: The dimension does not have to be greater than one for this rule to
: be workable. Is it possible to force this behaviour in Octave-1.0,
: or do I have to write a special-case "if ... " section to cope with
: the scalar case?