单点交叉

Single point crossover

我有两个数组(一行矩阵)temp1temp2 如下:

temp1=[1 2 3 4 5 6 7 8 9]
temp2=[10 11 12 13 14 15 16 17 18]

我有一个索引 pn=3。我需要如下输出:

tempNew=[1 2 3 13 14 15 16 17 18]

即我如何创建 tempNew 以便索引 pn 之前的所有值都来自 temp1 并且索引 pn 以外的所有值都来自 temp2?

temp1=[1 2 3 4 5 6 7 8 9]
temp2=[10 11 12 13 14 15 16 17 18]
pn=3;
tempNew = [temp1(1:pn),temp2(pn+1:end)]
tempNew =
     1     2     3    13    14    15    16    17    18

您使用 pn 创建两个临时数组来索引您的两个 tempX 数组。然后简单地使用方括号将它们连接起来。

在 MATLAB 中,索引总是从 1 开始,因此 1:pn 将为您提供数组的第一个 pn 值。 end 表示数组的结尾,因此 pn+1:end 将为您提供从索引 pn+1 到数组最后一个的所有值。