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

Updated: Randomly adding in rows to a matrix but following strict rules as to where

以下是更大矩阵的一部分:

     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

等..矩阵以同样的方式从这一点继续:

在其构造中,矩阵分为 4 行块,每个块由第一列中的升序数字标记 (0-0-0-0-1-1-1-1-2-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

• 在第 3 列包含数字 2

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

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

• 在第 7 列中包含数字 0.25

• 重要的是,插入点不应与 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

例如:

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

有人能帮忙吗?

这些要求不完整;例如,您永远不会在第 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];

据我所知,这应该可以满足您的要求。原始矩阵为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

这是一个使用较小数据集和 insertRange 的示例 运行。

>> 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