I had the following problem: I needed to select a number of indices in a large table and at the same time renumber them to get a more compact set. The excluded entries had to be set to zero. Well, a pretty specific and uncommon programming problem, but still
Here is a simple example:
Given the list of numbers 1, 2, 3, … 10, I need to select only 3,6, 4 and 9 (in that order, although the order is not really interesting in my case). So the result is: 0, 0, 1, 3, 0, 2, 0, 0, 4, 0. (That is: 3 occurs at the first position in the selection, so that becomes 1, 4 occurs on the third position, hence it becomes 3, etc.)
I found the following compact solution:
output = 0
output(selected) = [( i, i = 1,size(selected))]
You can of course do the same with a do-loop:
output = 0
do i = 1,size(selected)
output(selected(i)) = i
enddo
but the other solution is more fun.