Algorithm to create all possible combinations

前端 未结 2 1017
萌比男神i
萌比男神i 2021-01-21 12:02

I\'m writing a spares grid code and need to combine N 1-dimensional grid points (written in vector form) into the an array of all possible points. For example one can mix two ve

2条回答
  •  粉色の甜心
    2021-01-21 12:35

    I don't know Fortran, but since you say you can't find the underlying algorithm I'm assuming you will be able to write this yourself once you know the algorithm. It's quite easy actually. The pseudocode would be something like this (assuming there are no duplicates):

    index = 0   ! or 1
    for each element in first vector
        for each element in second vector
            matrix(index,1) = current element of first vector
            matrix(index,2) = current element of second vector
            index = index + 1
        end for
    end for
    

    This should give you a matrix similar to the one you would get using combvec. There are probably more efficient ways to do this, but as I don't know the details of Fortran I can't help you there unfortunately. In Matlab you would of course vectorize this.

    Good luck =)

提交回复
热议问题