更新:随机添加行到矩阵,但遵循严格的规则,在哪里



下面是一个更大的矩阵的一个块:

     0    1.0000    1.0000   77.0000  100.0000         0    0.2500
     0    1.0000    1.0000   72.0000  100.0000    0.2500    0.2500
     0    1.0000    1.0000   69.0000  100.0000    0.5000    0.2500
     0    1.0000    1.0000   48.0000  100.0000    0.7500    0.2500
1.0000    1.0000    1.0000   65.0000  100.0000    1.0000    0.2500
1.0000    1.0000    1.0000   71.0000  100.0000    1.2500    0.2500
1.0000    1.0000    1.0000   62.0000  100.0000    1.5000    0.2500
1.0000    1.0000    1.0000   41.0000  100.0000    1.7500    0.2500
2.0000    1.0000    1.0000   62.0000  100.0000    2.0000    0.2500
2.0000    1.0000    1.0000   67.0000  100.0000    2.2500    0.2500
2.0000    1.0000    1.0000   71.0000  100.0000    2.5000    0.2500
2.0000    1.0000    1.0000   43.0000  100.0000    2.7500    0.2500
3.0000    1.0000    1.0000   71.0000  100.0000    3.0000    0.2500
3.0000    1.0000    1.0000   62.0000  100.0000    3.2500    0.2500
3.0000    1.0000    1.0000   67.0000  100.0000    3.5000    0.2500
3.0000    1.0000    1.0000   47.0000  100.0000    3.7500    0.2500
4.0000    1.0000    1.0000   69.0000  100.0000    4.0000    0.2500
4.0000    1.0000    1.0000   65.0000  100.0000    4.2500    0.2500
4.0000    1.0000    1.0000   60.0000  100.0000    4.5000    0.2500
4.0000    1.0000    1.0000   41.0000  100.0000    4.7500    0.2500
5.0000    1.0000    1.0000   74.0000  100.0000    5.0000    0.2500
5.0000    1.0000    1.0000   71.0000  100.0000    5.2500    0.2500
5.0000    1.0000    1.0000   65.0000  100.0000    5.5000    0.2500
5.0000    1.0000    1.0000   47.0000  100.0000    5.7500    0.2500

等。《黑客帝国》从这里开始以同样的方式继续:

  • 第1列每4行上升1秒:0-0-0-0-1-1-1- 2-2-2…n-n-n-n
  • 第二列总是1
  • 第3列始终为1
  • 第4列以4个数字为组(例如[77 72 69 48]是第一组)
  • 第5列始终为100
  • 第6列每行递增0.25
  • 第7列始终为0.25

在其构造中,矩阵被分成4行块,每个块由第一列的升序数字标记(0-0-0-0-1-1-1- 2-2-2等)。例如,第一个块是:

 0    1.0000    1.0000   77.0000  100.0000         0    0.2500
 0    1.0000    1.0000   72.0000  100.0000    0.2500    0.2500
 0    1.0000    1.0000   69.0000  100.0000    0.5000    0.2500
 0    1.0000    1.0000   48.0000  100.0000    0.7500    0.2500

完整矩阵的长度将在1500标记附近,例如:1512

  • 我想在随机点插入另一行。它应该:

•在第4列中包含数字69

•在第三列中包含数字2

•在第一列中包含一个值,该值与前一行中的值相比为+6(即,如果前一行中的1列的值为'3',那么我希望当前行的1列的值为9)

•在第6列中包含一个值,该值在整个矩阵中保持连续上升0.25的模式,即0, 0.25。0.5、0.75(以下行中的值应进行调整,以继续这种模式)

•在第7列中包含一个数字0.25

  • 为了使事情更复杂,我实际上想多次这样做,而不仅仅是一次-也就是说,在整个矩阵中,我想插入许多与此描述匹配的单行。

  • 每个插入点与下一个插入点之间应该间隔原始矩阵的80到200行。然而,在每个实例中,数字应该随机化(即一行的第一次插入可能是在原始矩阵上的84行之后,对于下一次插入,这次可能是在第一次插入之后,例如,在第一次插入之后196行)。

•关键的是,插入点不能相交于一个4音符组:

。这是一个糟糕的插入点:

19.0000    1.0000    1.0000   72.0000  100.0000   19.0000    0.2500
19.0000    1.0000    1.0000   67.0000  100.0000   19.2500    0.2500
19.0000    1.0000    1.0000   76.0000  100.0000   19.5000    0.2500
19.0000    1.0000    1.0000   48.0000  100.0000   19.7500    0.2500
20.0000    1.0000    1.0000   65.0000  100.0000   20.0000    0.2500
20.0000    1.0000    1.0000   69.0000  100.0000   20.2500    0.2500
26.0000    1.0000    1.0000   69.0000  100.0000   20.5000    0.2500

但这是好的:

19.0000    1.0000    1.0000   72.0000  100.0000   19.0000    0.2500
19.0000    1.0000    1.0000   67.0000  100.0000   19.2500    0.2500
19.0000    1.0000    1.0000   76.0000  100.0000   19.5000    0.2500
19.0000    1.0000    1.0000   48.0000  100.0000   19.7500    0.2500
20.0000    1.0000    1.0000   65.0000  100.0000   20.0000    0.2500
20.0000    1.0000    1.0000   69.0000  100.0000   20.2500    0.2500
20.0000    1.0000    1.0000   60.0000  100.0000   20.5000    0.2500
20.0000    1.0000    1.0000   45.0000  100.0000   20.7500    0.2500
26.0000    1.0000    1.0000   69.0000  100.0000   21.0000    0.2500
  • 对于每一个插入的行:在插入行之后的第1列的所有值必须加11。
例如:

19.0000    1.0000    1.0000   72.0000  100.0000   19.0000    0.2500
19.0000    1.0000    1.0000   67.0000  100.0000   19.2500    0.2500
19.0000    1.0000    1.0000   76.0000  100.0000   19.5000    0.2500
19.0000    1.0000    1.0000   48.0000  100.0000   19.7500    0.2500
20.0000    1.0000    1.0000   65.0000  100.0000   20.0000    0.2500
20.0000    1.0000    1.0000   69.0000  100.0000   20.2500    0.2500
20.0000    1.0000    1.0000   60.0000  100.0000   20.5000    0.2500
20.0000    1.0000    1.0000   45.0000  100.0000   20.7500    0.2500
26.0000    1.0000    1.0000   69.0000  100.0000   21.0000    0.2500
32.0000    1.0000    1.0000   64.0000  100.0000   21.2500    0.2500
32.0000    1.0000    1.0000   67.0000  100.0000   21.5000    0.2500
32.0000    1.0000    1.0000   60.0000  100.0000   21.7500    0.2500
32.0000    1.0000    1.0000   36.0000  100.0000   22.0000    0.2500
33.0000    1.0000    1.0000   72.0000  100.0000   22.2500    0.2500
33.0000    1.0000    1.0000   67.0000  100.0000   22.5000    0.2500
33.0000    1.0000    1.0000   64.0000  100.0000   22.7500    0.2500
33.0000    1.0000    1.0000   43.0000  100.0000   23.0000    0.2500
  • 最后. .对于插入行和下一个插入行之间的每个矩阵块(也包括矩阵开始和第一个插入行之间的块,以及最后插入行和矩阵结束之间的块),我想在第4列的原始值中添加1到12之间的随机数。(以"2"、"9"one_answers"5"为"1至12之间的随机数"为例)'inserted_row - value+2 - value+2 - value+2 - value+2.... .Next_inserted_row - value+9 - value+9 - value+9…Next_inserted_row - value+5.."等)

有人能帮忙吗?

据我所知,这应该能满足您的要求。原矩阵为M:

insertRange = [80 200];   %// number of lines to skip before inserting
chunkStart = 0;
while chunkStart < size(M,1)
   chunkEnd = chunkStart + randi(insertRange/4) * 4;
   %// add random value to column 4
   addedValue = randi(12);
   lastRow = min(chunkEnd,size(M,1));
   M(chunkStart+1:lastRow,4) = M(chunkStart+1:lastRow,4) + addedValue;
   if chunkEnd < size(M,1)
      %// we haven't reached the end; insert new row after chunkEnd
      newRow = M(chunkEnd,:);
      newRow(1) = newRow(1) + 6;
      newRow(3) = 2.0;
      newRow(4) = 69.0;
      newRow(6) = newRow(6) + 0.25;
      %// now adjust remaining rows (> chunkEnd)
      M(chunkEnd+1:end,1) = M(chunkEnd+1:end,1) + 11.0;
      M(chunkEnd+1:end,6) = M(chunkEnd+1:end,6) + 0.25;
      M = [M(1:chunkEnd,:); newRow; M(chunkEnd+1:end, :)];
      chunkEnd = chunkEnd+1;
   end
   chunkStart = chunkEnd;
end

下面是使用较小的数据集和inserrange运行的示例。

>> OriginalM(36:46,:)
ans =
     8.00000     1.00000     1.00000    56.00000   100.00000     8.75000     0.25000
     9.00000     1.00000     1.00000    68.00000   100.00000     9.00000     0.25000
     9.00000     1.00000     1.00000    76.00000   100.00000     9.25000     0.25000
     9.00000     1.00000     1.00000    72.00000   100.00000     9.50000     0.25000
     9.00000     1.00000     1.00000    48.00000   100.00000     9.75000     0.25000
    10.00000     1.00000     1.00000    67.00000   100.00000    10.00000     0.25000
    10.00000     1.00000     1.00000    71.00000   100.00000    10.25000     0.25000
    10.00000     1.00000     1.00000    66.00000   100.00000    10.50000     0.25000
    10.00000     1.00000     1.00000    47.00000   100.00000    10.75000     0.25000
    11.00000     1.00000     1.00000    60.00000   100.00000    11.00000     0.25000
    11.00000     1.00000     1.00000    72.00000   100.00000    11.25000     0.25000

9组后插入新行后的输出:

>> M(36:46,:)
ans =
     8.00000     1.00000     1.00000    68.00000   100.00000     8.75000     0.25000
     9.00000     1.00000     1.00000    80.00000   100.00000     9.00000     0.25000
     9.00000     1.00000     1.00000    88.00000   100.00000     9.25000     0.25000
     9.00000     1.00000     1.00000    84.00000   100.00000     9.50000     0.25000
     9.00000     1.00000     1.00000    60.00000   100.00000     9.75000     0.25000
    15.00000     1.00000     2.00000    69.00000   100.00000    10.00000     0.25000
    21.00000     1.00000     1.00000    73.00000   100.00000    10.25000     0.25000
    21.00000     1.00000     1.00000    77.00000   100.00000    10.50000     0.25000
    21.00000     1.00000     1.00000    72.00000   100.00000    10.75000     0.25000
    21.00000     1.00000     1.00000    53.00000   100.00000    11.00000     0.25000
    22.00000     1.00000     1.00000    66.00000   100.00000    11.25000     0.25000

这些要求不完整;例如,永远不要在第2、第5、第6和第7列中指定想要的内容;对于这些,我简单地写上nan。我也同意后一点很令人困惑——在一些情况下,我不知道你说的"between"是什么意思。然而,我已经把(我认为你要求的)代码放在一起,并且在任何情况下都应该给你正确的想法。矩阵m0是解:

close all 
clear all
clc
%create q new rows in bunches of 4:
q = 20;
m0 =    [ 0    1.0000    1.0000   77.0000  100.0000         0    0.2500;...
     0    1.0000    1.0000   72.0000  100.0000    0.2500    0.2500;...
     0    1.0000    1.0000   69.0000  100.0000    0.5000    0.2500;...
     0    1.0000    1.0000   48.0000  100.0000    0.7500    0.2500;...
1.0000    1.0000    1.0000   65.0000  100.0000    1.0000    0.2500;...
1.0000    1.0000    1.0000   71.0000  100.0000    1.2500    0.2500;...
1.0000    1.0000    1.0000   62.0000  100.0000    1.5000    0.2500;...
1.0000    1.0000    1.0000   41.0000  100.0000    1.7500    0.2500;...
2.0000    1.0000    1.0000   62.0000  100.0000    2.0000    0.2500;...
2.0000    1.0000    1.0000   67.0000  100.0000    2.2500    0.2500;...
2.0000    1.0000    1.0000   71.0000  100.0000    2.5000    0.2500;...
2.0000    1.0000    1.0000   43.0000  100.0000    2.7500    0.2500;...
3.0000    1.0000    1.0000   71.0000  100.0000    3.0000    0.2500;...
3.0000    1.0000    1.0000   62.0000  100.0000    3.2500    0.2500;...
3.0000    1.0000    1.0000   67.0000  100.0000    3.5000    0.2500;...
3.0000    1.0000    1.0000   47.0000  100.0000    3.7500    0.2500;...
4.0000    1.0000    1.0000   69.0000  100.0000    4.0000    0.2500;...
4.0000    1.0000    1.0000   65.0000  100.0000    4.2500    0.2500;...
4.0000    1.0000    1.0000   60.0000  100.0000    4.5000    0.2500;...
4.0000    1.0000    1.0000   41.0000  100.0000    4.7500    0.2500;...
5.0000    1.0000    1.0000   74.0000  100.0000    5.0000    0.2500;...
5.0000    1.0000    1.0000   71.0000  100.0000    5.2500    0.2500;...
5.0000    1.0000    1.0000   65.0000  100.0000    5.5000    0.2500;...
5.0000    1.0000    1.0000   47.0000  100.0000    5.7500    0.2500];
L = length(m0);
%find random insert point:
insert_index = 4*(randi(L/4));
col1_val = m0(insert_index,1);
for j2 = 1:q
    added_random = randi(12);
    for j3 = 1:4
       index = (j2-1)*4 + j3;
       m_insert(index,:) = [ col1_val + 6, nan ,2 ,69 + added_random ,nan ,nan ,nan ];
    end 
    col1_val = col1_val + 1;
end
mfirst = m0(1:insert_index,:);
mlast = m0(insert_index+1:end,:);
m0 = [mfirst;m_insert;mlast];

最新更新