Intuitive numeric value for indicating ':' and 'end'

I am working on a mex routine that will accept indexing vectors. I would like this vector to be able to use the equivalent of ':' and 'end' with numeric values. I have two options I am considering:
Option (1) 0 = ':' , inf = 'end'
Example: Suppose x is a 2x3x4 array, then the equivalent of x(:,2,end) would be arguments (x,[0 2 inf])
Option (2) 0 = 'end' , inf = ':'
Example: Suppose x is a 2x3x4 array, then the equivalent of x(:,2,end) would be arguments (x,[inf 2 0])
I am leaning towards Option 1 for the method to be used for my mex routine, since inf seems to be intuitive for 'end' and that leaves 0 to mean ':'.
Does anyone have any other thoughts as to which method is more intuitive, or why one method might be more or less intuitive than the other? Or maybe there are already other functions out there that do something similar and a convention for this has already been established? Or maybe NaN values would be more intuitive for some reason? Etc.

5 commentaires

That's a generally difficult question to answer, as it is quite subjective. Some caution should be taken here, since users are likely to attempt to compute the endpoints of a range programmatically elsewhere and expect statements like ifinal = istop - 2 to work correctly when istop is your sentinel value. Arithmetic on Inf and NaN won't satisfy this need.
You could try extending your Option (1) to operate modulo the size of your array. So 0 would be equivalent to end, -1 would be equivalent to end-1, etc. You should take care to document that in triplicate though, since this introduces its own set of difficulty during debug (search for "negative indices python" to see some precedent for this).
Your approach may lead to an ambiguous input.
x = rand(2, 3, 4);
y = x(:, [1 2], [3 2]);
How would your MEX function parse [0 1 2 3 2]? As above or:
z = x(:, [1 2 3], 2);
You might want to accept an indexing struct array (as can be created using the substruct function) instead of a vector. This documentation page lists some cases you may need or want to consider.
If you move your comments to the Answers section I can vote on them and provide responses individually.
It is not uncommon in programming languages for -1 to indicate end, -2 the value before that and so on. It is uncommon for 0 to represent the end, especially since 0 is a common starting point.

Connectez-vous pour commenter.

Réponses (1)

I originally posted this as a comment rather than an answer because I hadn't thought at first that my message was an answer. It started as a request for clarification. But I suppose my suggestion about substruct counts as a potential answer.
Your approach may lead to an ambiguous input.
x = rand(2, 3, 4);
y = x(:, [1 2], [3 2]);
How would your MEX function parse [0 1 2 3 2]? As above or:
z = x(:, [1 2 3], 2);
You might want to accept an indexing struct array (as can be created using the substruct function) instead of a vector. This documentation page lists some cases you may need or want to consider.

1 commentaire

James Tursa
James Tursa le 20 Jan 2018
Modifié(e) : James Tursa le 20 Jan 2018
Thanks for the input. Expanding on the original question above, the mex function I am writing will only accept indexing that is contiguous for downstream processing, and I would like there to be an easy and intuitive way for the user to enter that indexing. My original thought was to have the user enter the starting and ending indexing as two separate vectors, with the added ':' and 'end' capabilities noted in my original question.
So, for your examples above:
x(:,[1 2],[3 2]) is a discontiguous subarray of x and would be disallowed, so doesn't apply.
x(:,[1 2 3],2) is a contiguous subarray of x and would be allowed.
The interface I am thinking of would be to specify the starting and ending index vectors as arguments to the mex routine. If we assume that inf means ':' and 0 means 'end' per Greg's comments, then the indexing arguments would be as follows for this example:
mexroutine(x,[inf 1 2],[inf 3 2])
Or, it could have been specified as follows:
mexroutine(x,[inf inf 2])
I think I will go with Greg's excellent suggestion of having 0 mean 'end' and do negative inputs in a modulo way, leaving inf to mean ':'.
I will have to do some more thinking about whether this two vector approach to starting and ending indexing (which is easier to work with in a mex routine) is sufficient, or whether I will need to look into something else (e.g., a MATLAB indexing struct) that may be more intuitive for the user.

Connectez-vous pour commenter.

Catégories

Tags

Commenté :

le 30 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by