Intuitive numeric value for indicating ':' and 'end'
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Walter Roberson
2018 年 1 月 20 日
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.
回答 (1 件)
Steven Lord
2018 年 1 月 20 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!