一次性随机 [英] One-time randomization

查看:89
本文介绍了一次性随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵ECGsig,每行包含一个1秒长的ECG信号,

I have a matrix, ECGsig, with each row containing a 1-second-long ECG signal,

稍后我将对它们进行分类,但是我想随机更改行,例如

I will classify them later but I want to randomly change the rows like,

idx = randperm(size(ECGsig,1));
ECGsig = ECGsig(idx,:);

但是我希望这种情况只发生一次,而不是每次运行程序时都会发生,

However I want this to happen just once and not every time that I run the program,

或者换句话说,只产生一次随机数,

Or in other words to have the random numbers generated only once,

因为如果每次更改都会有不同的分类结果,

Because if it changes every time I would have different results for classification,

除了在单独的m文件中保存并将其保存在mat文件中之外,还有其他方法吗?

Is there any way to do this beside doing in a separate m file and saving it in a mat file?

谢谢

推荐答案

您可以设置随机生成种子,以便每次运行随机结果时,每次都会生成相同随机结果.您可以通过 rng 来执行此操作.这样,即使多次运行该程序,无论如何它仍然会生成相同的随机序列.因此,请尝试执行以下操作:

You can set the random generation seed so that every time you run a random result, it will generate the same random result each time. You can do this through rng. This way, even though run the program multiple times, it will still generate the same random sequence regardless. As such, try doing something like:

rng(1234);

rng的输入将是种子.但是,根据Luis Mendo的评论,rng仅在更新版本的MATLAB中可用.如果rng在您的MATLAB发行版中不可用,请执行以下操作:

The input into rng would be the seed. However, as per Luis Mendo's comment, rng is only available with newer versions of MATLAB. Should rng not be available with your distribution of MATLAB, do this instead:

rand('seed', 1234);

您还可以查看 randstream ,但是这有点太先进了,所以我们现在就不要看它.要将种子重置为打开MATLAB之前的种子,请选择种子0.因此:

You can also take a look at randstream, but that's a bit too advanced so let's not look at it right now. To reset the seed to what it was before you opened MATLAB, choose a seed of 0. Therefore:

rng(0); %// or
rand('seed', 0);

通过调用此方法,您从此点生成的任何随机结果都将基于预定顺序.种子可以是您真正想要的任何整数,但是要使用您会记住的东西.在执行任何操作之前,请将其放置在代码的开头.我们控制随机数生成方式的主要原因是,这鼓励产生可重复的结果和研究.这样,如果您决定随机或随机执行任何操作,其他人就可以生成您创建的结果.

By calling this, any random results you generate from this point will be based on a pre-determined order. The seed can be any integer you want really, but use something that you'll remember. Place this at the very beginning of your code before you do anything. The main reason why we have control over how random numbers are generated is because this encourages the production of reproducible results and research. This way, other people can generate the results you have created should you decide to do anything with random or randomizing.

即使您说只想运行一次此随机化操作,也可以避免在多次运行程序之前将结果保存到另一个文件中的麻烦.通过设置种子,即使您多次运行该程序,也可以确保每次都生成相同的随机序列.

Even though you said you only want to run this randomization once, this will save you the headache of saving your results to a different file before you run the program multiple times. By setting the seed, even though you're running the program multiple times, you're guaranteed to generate the same random sequence each time.

这篇关于一次性随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆