The first solution in the original submission is a little ugly.
If we didn't want to use the second solution (using "repelems"),
here is a third solution.
Besides, in fact, in the subfuntion prepareIdx, the variable "idx" is actually a row vector
except for the last line "idx = (find (v != 0))(cumsum (idx))".
The problem is that "find(v!=0)" might be a row or column vector according to the shape of "v".
If we really wanted the output "idx" to be a row vector,
we could change that line into "idx = find (v != 0)(:).'(cumsum (idx))".
|
For example,
One possible solution is as follow.
(Besides, note that the comment "idx2 output will be a row vector" is wrong; in fact the output can be either row or column vector.)
Another solution is to use built-in function "repelems" directly.
And below is some proposed test case.
|