更新:将行随机添加到矩阵中,但遵循严格的位置规则 [英] Updated: Randomly adding in rows to a matrix but following strict rules as to where

查看:68
本文介绍了更新:将行随机添加到矩阵中,但遵循严格的位置规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

     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行以1s上升:0-0-0-0-1-1-1-1-2-2-2-2 ... n-n-n-n
  • 第2列始终为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-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

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

•在第四列中包含数字69

•在第三列中包含数字2

•在第一列中包含的值比上一行中的值+6(即,如果上一行中的第1列的值为"3",那么我希望当前行中的第1列具有值9)

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

•在第7列中包含数字0.25

  • 为了使事情变得更复杂,我实际上想多次执行此操作,而不仅仅是一次-也就是说,在整个矩阵中,我要插入许多与此描述匹配的单行. /p>

  • 每个插入点应与下一个插入点隔开原始矩阵的80至200行之间的某个位置.但是,在每种情况下,应随机分配80至200之间的 the 数量 行(即,第一次插入该行可能在原始矩阵的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

    最后,对于插入的行和下一个插入的行之间的矩阵的每个块(还包括矩阵开始和第一个插入的行之间的块,以及最后插入的行和矩阵的结束之间的块)I' d希望将第1列和第12列之间的随机数添加到第4列的原始值中.(例如(在示例中,"2","9"和"5"是"1到12之间的随机数") -值+2-值+2-值+2-值+2 .... next_inserted_row-值+9-值+9-值+9 ... next_inserted_row-值+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

这里是使用较小数据集和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

The following is a chunk of a much larger Matrix:

     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

etc.. the Matrix continues from this point in the same fashion:

  • column 1 ascends in 1s every 4 rows: 0-0-0-0-1-1-1-1-2-2-2-2...n-n-n-n
  • column 2 is always 1
  • column 3 is always 1
  • column 4 is grouped in sets of 4 numbers (e.g. [77 72 69 48] is the first set)
  • column 5 is always 100
  • column 6 ascends by 0.25 each row
  • column 7 is always 0.25

In its construction the matrix is grouped in 4 row chunks, each signposted by the ascending number in the first column (0-0-0-0-1-1-1-1-2-2-2-2 etc). For example, the first chunk is:

 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

The length of the full matrix will be around the 1500 mark, let's say: 1512

  • I'd like to insert another row at a random point. It should:

• contain the number 69 in the 4th column

• contain the number 2 in the 3rd column

• contain a value in the 1st column that is +6 from that in the preceding row (i.e. if column 1 in the previous row had the value '3' then I'd want column 1 in the current row to have the value 9)

• contain a value in the 6th column that maintains the pattern of consecutively rising by 0.25 throughout the whole matrix, ie. 0, 0.25. 0.5, 0.75 (and the values in the following rows should be adjusted to continue the pattern as such)

• contain a number 0.25 in the 7th column

  • to make things more complicated I actually want to do this a number of times, rather than just the once - which is to say that throughout the matrix I want to insert lots of single rows that match this description.

  • Each insertion point should be separated from the next by somewhere between 80 to 200 rows of the original matrix. However in each instance the number of rows between 80 and 200 should be randomised (i.e. the first insertion of a row might be after, say, 84 rows on the original matrix, for the next insertion this time it might be after, say, 196 after the first one).

• Crucially, the insertion point should not intersect a 4 note group:

i.e. this is a bad insertion point:

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

But this is fine:

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

  • For each inserted row: all values in column 1 that follow the insertion row must have 11 added to them.

For example:

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

  • Finally.. for every chunk of matrix between an inserted row and the next inserted row (also including the chunk between the matrix beginning and the first inserted row, and the chunk between the last inserted row and the matrix end) I'd like to add to the original values in column 4 a random number between 1 and 12. (as an example (with '2' '9' and '5' in the example being 'random numbers between 1 and 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..' etc)

Is anyone able to help with that?

解决方案

As far as I can determine, this should give what you're asking for. The original matrix is 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

Here's a sample run using a smaller data set and 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

and the output with the new row inserted after the 9 group:

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

这篇关于更新:将行随机添加到矩阵中,但遵循严格的位置规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆