【问题标题】:generate a random square matrix with condition on upper diagonal elements生成具有上对角元素条件的随机方阵
【发布时间】:2016-08-04 09:20:24
【问题描述】:

我想在 MATLAB 中生成一个随机方阵,其上对角线的元素个数相等,小于 0.5 且大于 0.5。我想保持所有对角线元素等于零,并且对角线下方(下三角形)与对角线上方(上三角形)元素相同。所以预期的输出可能如下所示

  0 a1 a2 a3
  a1 0 a4 a5
  a2 a4 0 a6
  a3 a5 a6 0

其中 ai 是在 (0,0.5) 和 (0.5,1) 之间平均分布的随机数。

【问题讨论】:

  • 再来一次??顺便说一句,下对角线和对角线元素呢?在你的问题中写一个预期输出的例子!
  • 随机数不能均匀分布,其中一半大于 0.5,另一半小于 0.5。
  • @patrik 例如,一个 4*4 矩阵将在对角线上方有六个元素。是否有可能有 3 个小于 0.5 和 3 个大于 0.5?
  • @bilal 是的,但如果您希望它们均匀分布随机数,则不是。如果您有任何条件需要强制执行这些条件,这将减少随机性。如果您指定如何解决此问题,我们可能会提供帮助。例如,您可以生成 2 组随机数 (0,0.5) 和 (0.5,1) 并将它们随机分布在矩阵中。
  • @patrik 很抱歉没有让自己说清楚。我想强加条件具有相等数量的元素小于 0.5 和大于 0.5

标签: matlab matrix random


【解决方案1】:

您写道,您希望“在对角线下方(下三角形)[元素是] 与对角线上方(上三角形)元素相同。”以后你可以说你希望一个矩阵是“对称的”,这就很清楚了。

我写了一些我认为可以满足你的目的的代码:

function mat = generate_matrix( n )

% "n" is the number of rows / columns the output named "mat" will have.

sml_val_min = 0;
sml_val_max = 0.5;
big_val_min = 0.5;
big_val_max = 1;

% The number of elements in the upper triangular portion of the matrix can be
% represented by an Arithmetic Series.
% There is a row with one element, then a row with 2 elements, then a row
% with 3, on up to a row with n - 1 elements.
% So, there are 1 + 2 + ... + n-1 = (n-1)*n/2 elements in total. 

numel_in_up_tri_portion = (n-1)*n/2;

% determine how many distinct small values we need to generate and how many large values
numel_sml = floor(numel_in_up_tri_portion/2);
numel_big = ceil(numel_in_up_tri_portion/2);

% Generate small values and large values
sml_vals = (sml_val_max - sml_val_min).*rand(1, numel_sml) + sml_val_min;
big_vals = (big_val_max - big_val_min).*rand(1, numel_big) + big_val_min;

% decide where in the matrix small values will go and where large values will go
sml_val_locations = randsample(numel_in_up_tri_portion, numel_sml);
big_val_locations = setdiff(1:numel_in_up_tri_portion, sml_val_locations);

% 
[rs, cs] = convert_linear_index_to_row_and_col( sml_val_locations );
[rb, cb] = convert_linear_index_to_row_and_col( big_val_locations );

mat = zeros(n);
for k = 1:numel_sml
    mat(rs(k), cs(k)) = sml_vals(k);
end
for k = 1:numel_big
    mat(rb(k), cb(k)) = big_vals(k);
end

mat = mat + mat';

end % of function definition  


function [row, col] = convert_linear_index_to_row_and_col( lin_ind )

unity = ones(size(lin_ind)); 

% Solve the quadratic equation (row^2 - row)/2 = lin_ind
row = ceil(0.5 * (unity + (unity + 8*lin_ind).^0.5));

%
col = lin_ind - 0.5*(row - unity).*(row - 2*unity);

end % of function definition    

【讨论】:

    【解决方案2】:

    可以生成 2 组同样大的随机数 (0,0.5) 和 (0.5,1) 并将它们打乱到矩阵中。

    function test()
    upperMatrix = triu(ones(4),1);
    r0 = 0.5*rand(3,1); % smaller set
    r1 = 0.5*rand(3,1)+0.5; % larger set
    rfull = [r0;r1] % full set
    permutation = randperm(6) % generate a scrambled vector of random number 1:6
    
    upperMatrix(upperMatrix == 1) = rfull(permutation) % insert values to upper matrix
    upperMatrix = upperMatrix + upperMatrix.' % make the matrix symmetric.
    

    请注意,我假设有偶数个唯一的非零元素。这就是问题表述中所述的内容,如果您需要其他内容,则需要应用特殊情况。例如,可以将最后一个元素设置为 0.5,或者添加一个额外的 (0.5,1) 数字或额外的 (0,0.5) 数字,或者您可以想出并为您工作的其他东西。

    【讨论】:

    • @Sardar_Usama OP 要求一半的数字应该从 (0,0.5) 设置,另一半在 (0.5,1) 之间设置。函数rand() 从[0,1] 范围内的均匀分布生成数字,这意味着它的一般公式是mu + (b-a)*rand(),其中mu 是平均值,b-a 是范围。实际上,由于数字在matlab中分布在[0,1]范围内,因此公式变为a+(b-a)*rand()。如果您对此表示怀疑,您可以进行测试,但这就是必须的。
    【解决方案3】:
    req=zeros(n);    %initializing a matrix of size nxn
    
    %finding the indexes of upper diagonal elements
    Up_tri_ind = find(triu(true(size(req)), 1));
    
    %finding the number of upper diagonal elements
    temp=length(Up_tri_ind);
    
    %Filling first half of the indexes with values less than 0.5 
    %and the other half with greater than 0.5
    req(Up_tri_ind(1:temp/2))= 0.5*rand(1,temp/2);
    req(Up_tri_ind(temp/2+1:end))= 0.5+0.5*rand(1,temp/2);
    
    %Replacing the same elements in the lower diagonal
    req=req+req.';
    

    请注意,这仅在上对角线或下对角线中的元素数为偶数时才有可能,因为偶数的一半也是整数,而奇数的一半绝不是整数。

    因此只有当 n 属于以下系列时才有可能:
    ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍ ‍‍‍‍‍ ‍‍ ‍‍‍‍‍‍ ‍‍4,5,8,9,12,13,16,17,.... >

    【讨论】:

    • 非常感谢。是的,最后一点是我正在研究的关于矩阵大小的问题。欢呼
    猜你喜欢
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 2012-04-02
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    相关资源
    最近更新 更多