如何将矩阵 R 放入循环中并生成矩阵 R> 的新值

How can I put matrix R in a loop and generate new values of matrix R>

我有一个矩阵 R,它的对角矩阵 0 和行的总和 应该总是一个 。我怎样才能更新矩阵,只有创建 sum = 1 的值应该在迭代后改变。我怎样才能用 for 循环做到这一点?

 R = M(1:end, 1:end-1)
 R =
   0.00000   0.22013   0.59930   0.00000   0.00000   0.18057
   0.00000   0.00000   0.22196   0.00000   0.77804   0.00000
   0.00000   0.00000   0.00000   0.60774   0.00000   0.39226
   0.00000   0.00000   0.17406   0.00000   0.09191   0.00000
   0.73861   0.11227   0.00000   0.00000   0.00000   0.14912
   0.00000   0.26643   0.24617   0.15687   0.00000   0.00000

这是我目前的R。我只希望 0.22013 0.599300.18057 等值通过 for 循环迭代更改为另一个变体。

在 MATLAB 中:

新生成的数组,各行总和为 1:

此解决方案侧重于三个主要领域:

• 在每行中查找 non-zero 个索引。

这是通过使用基于条件 (R ~= 0)find() 函数完成的,结果是 find(R ~= 0),其中 returns 索引是 non-zero。

• 创建一组总和为 1 且集合大小等于 non-zero 索引数量的随机数。

使用函数rand()可以生成一组数字。为确保数字集加起来为 1,我们可以除以生成集的 sum()。设置大小可以由第二个输入参数指定。在这种情况下,我们希望集合大小为 1 乘以给定行中 non-zero 索引的数量。这样我们就可以逐行替换 for-loop 中的值。该调用看起来类似于 rand(1,Number_Of_Non_Zero_Indices)rand(1,length(Non_Zero_Indices).

• 设置新创建的值集以替换旧的 non-zero 值。

为了替换旧的 non-zero 值,我们使用 find() 函数生成的索引和矩阵索引矩阵 R 的给定行。要逐行替换值,矩阵索引调用方式为:

R(Row,Non_Zero_Indicies) = Random_Set;

在哪里, Row 是 for-loop 中的循环变量, Non_Zero_Indicies 是由 find() 函数生成的索引。这允许结果集替换相应的索引。

完整脚本:

%Initializing the matrix%
R = [
   0.00000   0.22013   0.59930   0.00000   0.00000   0.18057;
   0.00000   0.00000   0.22196   0.00000   0.77804   0.00000;
   0.00000   0.00000   0.00000   0.60774   0.00000   0.39226;
   0.00000   0.00000   0.17406   0.00000   0.09191   0.00000;
   0.73861   0.11227   0.00000   0.00000   0.00000   0.14912;
   0.00000   0.26643   0.24617   0.15687   0.00000   0.00000
   ];

[Number_Of_Rows,Number_Of_Columns] = size(R); 

Number_Of_Iterations = 5;
Iteration = 1;

while(Iteration < Number_Of_Iterations)
for Row = 1: Number_Of_Rows

%Grabbing the row indices%   
Row_Vector = R(Row,:);

%Finding non-zero indices%
Non_Zero_Indicies = find(Row_Vector ~= 0);

%Generating random set of numbers%
Random_Set = rand(1, length(Non_Zero_Indicies)); 

%Normalizing the set so that the values add to 1%
Normalization_Factor = sum(Random_Set);
Random_Set = Random_Set / Normalization_Factor;

%Using matrix indexing to replace the values%
R(Row,Non_Zero_Indicies) = Random_Set;
 
R

end

Iteration = Iteration + 1;
end

R新生成的变体数量可以通过修改变量Number_Of_Iterations来改变。

函数形式:

要清理代码,可以将数组生成脚本放入函数中。可以迭代调用此函数以根据需要生成尽可能多的变体。

函数调用:

%Initializing the matrix%
R = [
   0.00000   0.22013   0.59930   0.00000   0.00000   0.18057;
   0.00000   0.00000   0.22196   0.00000   0.77804   0.00000;
   0.00000   0.00000   0.00000   0.60774   0.00000   0.39226;
   0.00000   0.00000   0.17406   0.00000   0.09191   0.00000;
   0.73861   0.11227   0.00000   0.00000   0.00000   0.14912;
   0.00000   0.26643   0.24617   0.15687   0.00000   0.00000
   ];

[R] = Generate_New_Array(R);

R

函数:

function [R] = Generate_New_Array(R)

[Number_Of_Rows,~] = size(R); 
for Row = 1: Number_Of_Rows

%Grabbing the row indices%   
Row_Vector = R(Row,:);

%Finding non-zero indices%
Non_Zero_Indicies = find(Row_Vector ~= 0);

%Generating random set of numbers%
Random_Set = rand(1, length(Non_Zero_Indicies)); 

%Normalizing the set so that the values add to 1%
Normalization_Factor = sum(Random_Set);
Random_Set = Random_Set / Normalization_Factor;

%Using matrix indexing to replace the values%
R(Row,Non_Zero_Indicies) = Random_Set;
 
end
end

运行 使用 MATLAB R2019b