首页 > 解决方案 > 如何将矩阵 R 放入循环中并生成矩阵 R> 的新值

问题描述

我有一个R具有对角矩阵的矩阵0,并且行的总和应始终为 1。如何更新矩阵,使得只有创建的值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 循环迭代更改为另一个变体。

标签: matlabmatrixnetworkingqueueing

解决方案


在 MATLAB 中:

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

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

• 查找每行中的非零索引。

这是通过使用find()基于返回非零索引的条件(R ~= 0)的函数来完成的。find(R ~= 0)

• 创建一组总和为 1 且设置大小等于非零索引数量的随机数。

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

• 设置新创建的值集以替换旧的非零值。

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

R(Row,Non_Zero_Indicies) = Random_Set;

其中, Row是 for 循环中的循环变量, 是函数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 运行


推荐阅读