【问题标题】:Matlab generate multiple random matricesMatlab生成多个随机矩阵
【发布时间】:2016-02-15 22:07:30
【问题描述】:

我必须生成 20 个大小不断增加的随机矩阵

200:50:1150 //sizes of 20 random matrices

我想将它们存储在矩阵数组中:

喜欢

array(1) // should give me the 1st matrix of size 200x200
array(2) // should give me the 2nd matrix of size 250x250 and so on

我不知道该怎么做:

n = 200:50:1150
for i=1:20
  M(:,:,i) = rand(n(i));   //This does not work
end

我该怎么做,有没有没有循环的更快方法?

【问题讨论】:

  • 代码示例不工作。请修复它或描述您期望的输出。
  • 这里我编辑了问题

标签: matlab matrix


【解决方案1】:

您不能将不同大小的矩阵堆叠成 3d 矩阵,矩阵具有固定尺寸。改用元胞数组:

n = 200:50:1150;
M=cell(1,numel(n));
for ix=1:numel(n);
  M{ix} = rand(n(ix)); 
end

不使用 for 循环不会提高性能。只需在一次调用中生成相同数量的随机数需要相同的时间:rand(sum(n.^2),1);

【讨论】:

    猜你喜欢
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-22
    • 2013-02-08
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2012-04-10
    相关资源
    最近更新 更多