为什么随机化不起作用(MATLAB)?(Why won't randomization work (MATLAB)?)

编程入门 行业动态 更新时间:2024-10-19 10:23:22
为什么随机化不起作用(MATLAB)?(Why won't randomization work (MATLAB)?)

好吧,这就像第五次我不得不提出这个问题,但仍然没有人能够给我一个答案或解决方案。 但是我们又来了......

我想运行一个非常简单的小MATLAB程序。 它只是随机显示目录中的图像。 这是我的代码:

files = dir(fullfile(matlabroot,'toolbox','semjudge',bpic,'*.png')); nFiles = numel(files); combos = nchoosek(1:nFiles, 2); index = combos(randperm(size(combos, 1)), :); picture1 = files(index(nRep,1)).name; picture2 = files(index(nRep,2)).name; image1 = fullfile(matlabroot,'toolbox','semjudge',bpic,picture1); image2 = fullfile(matlabroot,'toolbox','semjudge',bpic,picture2); subplot(1,2,1); imshow(image1); subplot(1,2,2); imshow(image2);

我尝试了几种不同的迭代,包括用“randsample”替换“nchoosek”。

但它不起作用! 每次运行程序时,脚本都以相同的顺序运行相同的图像文件。 它为什么这样做? 就像它第一次运行它时随机化图像文件,但现在它只按顺序运行它们,而不是每次运行脚本时随机化它们。

有人可以帮我这个吗?

Okay, this is like the 5th time I have had to ask this question, and still nobody has been able to give me an answer or solution. But here we go again ...

I want to run a very simple little MATLAB program. All it does is RANDOMLY display images from a directory. Here is my code:

files = dir(fullfile(matlabroot,'toolbox','semjudge',bpic,'*.png')); nFiles = numel(files); combos = nchoosek(1:nFiles, 2); index = combos(randperm(size(combos, 1)), :); picture1 = files(index(nRep,1)).name; picture2 = files(index(nRep,2)).name; image1 = fullfile(matlabroot,'toolbox','semjudge',bpic,picture1); image2 = fullfile(matlabroot,'toolbox','semjudge',bpic,picture2); subplot(1,2,1); imshow(image1); subplot(1,2,2); imshow(image2);

I have tried several different iterations of this, including replacing "nchoosek" with "randsample."

But it doesn't work! Every time I run the program, the script runs the same image files in the same order. Why is it doing this? It's like it randomized the image files the first time I ran it, but now it only runs them in that order, instead of randomizing them every time the script is run.

Can somebody please help me with this?

最满意答案

伪随机数生成器从特定种子开始 。 提供的“随机”数字是确定性的。 您需要更改种子以更改这些数字。

这样做的好处是,即使您在算法中使用伪随机性,也可以通过再次使用相同的种子来重放运行。

参考: http : //www.mathworks.de/help/techdoc/ref/rng.html

The pseudo-random number generator starts off from a specific seed. The "random" numbers provided are deterministic. You need to change the seed to change these numbers.

The benefit of this is that even if you use pseudo-randomness in your algorithm, you can always replay a run by using the same seed again.

Reference: http://www.mathworks.de/help/techdoc/ref/rng.html

更多推荐

本文发布于:2023-08-04 03:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   随机化   MATLAB   randomization   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!