【问题标题】:Matlab random samplingMatlab 随机抽样
【发布时间】:2016-10-12 12:55:20
【问题描述】:

我正在尝试在 Matlab 中锻炼自己。我正在尝试从名为 data.dat 的文件中随机选择两行。

我的 data.dat 文件如下所示:

12 4 6.1 7

14 4 8.4 62

7 56.1 75 98

9.7 54 12 35

2 4 8 7.8

从 data.dat 中随机选择 2 行是我的操作:

close all;
clear all;
%----------------------%
% Choose random lines
%----------------------%
M  = load('data.dat');
N=2; %number_of_lines
file_number = 2; %save each two lines in new file: selection_1, selection_2

现在我将两个选定的行顺序保存在新文件中。

for k = 1:file_number
 i = randi(length(M),N);
 B=M(i,:)
 filename=['samples_',int2str(k),'_mc.dat']
 save (filename, 'B', '-ascii')
 clear B;
 end

我不知道为什么,但每个新文件中有超过 2 行。你能解释一下我在哪里做错了吗?

【问题讨论】:

  • i = randi(length(M),N); 为您提供i 什么? M(i,:) 是 2 行吗?

标签: matlab random


【解决方案1】:

我认为您在生成随机数时犯了一个错误,如 GameOfThrows 所示。

i = randi(length(M),N); % gives you a matrix NxN of numbers
i = randi(length(M),[N,1]); % gives you a column of N numbers

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多