【问题标题】:Storing numbers temporarily in MATLAB在 MATLAB 中临时存储数字
【发布时间】:2014-11-05 06:28:17
【问题描述】:

在 MATLAB 中将数字存储在内存中但然后在 for 循环期间仅将它们的特定子集写入矩阵/数组的最佳方法是什么?我也在努力为我的问题设置一个合适的 for 循环:

我有一条特定长度的线 (x=100),以及特定位置的障碍 (pos=50)。我想进行多轮采样(在本例中为 1000 轮),并在每轮中生成特定数量的随机数 (p)。在显示的图像中,生成了 4 个数字,我希望 MATLAB 在 p > pos 时取最小的数字,在 p

这可以在 MATLAB 中实现吗?我做到了:

x = 100;
sample = 1000; %number of rounds of sampling to do
pos = 50; %position of barrier 
nn = 1:12; %the number of random numbers to generate for each round of sampling i.e. 1000 rounds generating 1 number, 1000 rounds generating 2 numbers and so on
len1 = 0;
len2= x ;
for i = 1:sample
    p(i) = int16(rand()*x)
    if (p(i)<pos) && (p(i)>len1) 
        len(i) = p(i)
    end
end

我知道我需要第二个 if 语句,类似于

if (p(i)>pos) && (p(i)<len2)

我的想法是在使用默认 len1 和 len2 值重新启动循环之前,用每轮采样期间生成的最小和最大数字临时替换 len1 和 len2 的值。不过可能有更好的方法。

任何帮助将不胜感激,

【问题讨论】:

  • 如果您生成的所有随机数都位于障碍的一侧会怎样?
  • 对不起 - 我忘了提到这种可能性。在这种情况下,len1 或 len2 的原始值仍将保留并用于减法。因此,如果 'b' 是唯一生成的数字,则输出将为 100-b。
  • 变量nn有什么作用?看起来你不使用它。另外,变量sam 是什么?和sample一样吗?
  • 太好了,感谢您的编辑。那么我是否清楚这一点 - 在此示例中,您正在生成 1000 个点,并且您想选择障碍物右侧的最小点和障碍物左侧的最大点,并记录它们的差异?
  • 是的 - sam = sample,我已经更新了代码。我还没有使用“nn”,因为我不知道该怎么做。 'nn' 包含我想在每批采样中生成的随机数的数量。例如,如果 nn=1:2 和 sample = 1000,我希望 MATLAB 执行 1000 轮,它只生成 1 个数字,存储这些结果,然后重复此操作,但生成 2 个数字。

标签: matlab


【解决方案1】:

如果我正确理解你的问题,你应该这样做

x = 100;       % length of the grid
pos = 50;      % position of the barrier
len1 = 0;      % left edge of the grid
len2 = x;      % right edge of the grid

sample = 1000; % number of samples to make
nn     = 1:12  % number of points to generate (will loop over these)

len = zeros(sample, length(nn)); % array to record the results

for n = 1:length(nn)                     % For each number of pts to generate
    numpts = nn(n);
    for i = 1:sample                     % For each round of sampling,
        p = round(rand(numpts,1) * x);   % generate 'numpts' random points.
        if any(p>pos)                    % If any are to the right of the barrier,
            pright = min(p(p>pos));      % pick the smallest.
        else
            pright = len2;
        end
        if any(p<pos)                    % If any are to the left of the barrier,
            pleft = max(p(p<pos));       % pick the largest.
        else
            pleft = len1;
        end
        len(i,n) = pright - pleft;       % Record the length of the interval.
    end
end

【讨论】:

  • 太棒了!这是完美的谢谢。这教会了我很多东西,尤其是关于嵌套 for 循环。
【解决方案2】:

这并没有解决您问题中的所有要点(临时存储数字的最佳方式),但显示了一种矢量化方法,如果您有随机数列表。据我所知,这是你所要求的一部分。

n = 1000;         %// Number of random numbers
max_val = 100;    %// Max value
min_val = 0;      %// Min value
p = 50;           %// Border

x = rand(n,1)*(max_val-min_val)+min_val;  %// Vector of random numbers
s = sort([min_val; x; max_val]-p);        %// Sort vector, with border "shifted" to 0
ind = find(diff(sign(s)));        %// Find index of closest number smaller than p
res =  diff(s(ind:ind+1))    %// Difference between closest numbers on each side of p

【讨论】:

  • 这真的很有趣。感谢您添加它。我完全不知道你可以这样做。
猜你喜欢
  • 2018-10-26
  • 2013-07-28
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多