在matlab中生成随机负数到正数的浮点数

时间:2016-04-25 16:58:07

标签: matlab random floating-point negative-number

如何在-0.01和0.01之间生成随机值?这不起作用:

rand()-(0.01);

我在互联网上发现的许多其他人都不能工作。这似乎很容易,但我快要发疯了。

2 个答案:

答案 0 :(得分:2)

来自documentation

  

通常,您可以在区间(a,b)中生成N个随机数   用公式:

     

10

在您的部分案例中,如果您希望在(-0.01, 0.01)区间内生成r = -0.01 + (0.01-(-0.01)).*rand(10,1) 个随机数,您可以这样做:

r =

    0.0081
   -0.0075
    0.0083
    0.0026
   -0.0080
   -0.0044
    0.0009
    0.0092
    0.0093
   -0.0068

给出:

FROM python:2.7-onbuild

MAINTAINER Ewan Valentine <ewan@theladbible.com>

# Various Python and C/build deps
RUN apt-get update && apt-get install -y \ 
    wget \
    build-essential \ 
    cmake \ 
    git \
    pkg-config \
    python-dev \ 
    python-opencv \ 
    libopencv-dev \ 
    libav-tools  \ 
    libjpeg-dev \ 
    libpng-dev \ 
    libtiff-dev \ 
    libjasper-dev \ 
    libgtk2.0-dev \ 
    python-numpy \ 
    python-pycurl \ 
    libatlas-base-dev \
    gfortran \
    webp \ 
    python-opencv 

# Install Open CV - Warning, this takes absolutely forever
RUN cd ~ && git clone https://github.com/Itseez/opencv.git && \ 
    cd opencv && \
    git checkout 3.0.0 && \
    cd ~ && git clone https://github.com/Itseez/opencv_contrib.git && \
    cd opencv_contrib && \
    git checkout 3.0.0 && \
    cd ~/opencv && mkdir -p build && cd build && \
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \ 
    -D INSTALL_C_EXAMPLES=ON \ 
    -D INSTALL_PYTHON_EXAMPLES=ON \ 
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ 
    -D BUILD_EXAMPLES=OFF .. && \
    make -j4 && \
    make install && \ 
    ldconfig

答案 1 :(得分:1)

<?php class Csv extends CI_Controller { function __construct() { parent::__construct(); } function index() { $data = array('title' => 'Upload CSV Contacts'); $this->load->view('csvupload_view', $data); } function import_csv() { $data['error'] = ''; //initialize image upload error array to empty $config['upload_path'] = './assets/extra/member_upload/'; $config['allowed_types'] = 'csv'; $config['max_size'] = '1000'; $this->load->library('upload', $config); // If upload failed, display error if (!$this->upload->do_upload()) { $data['error'] = $this->upload->display_errors(); $this->load->view('csvupload_view', $data); } else { $file_data = $this->upload->data(); $file_path = './assets/extra/member_upload/'.$file_data['file_name']; if ($this->csvimport->get_array($file_path)) { $csv_array = $this->csvimport->get_array($file_path); $this->load->model('csv_model'); //convert array of arrays into array foreach($csv_array as $key=>$val) { $csv_array[$key] = array('email' => $val['Email'], 'name' => $val['Name']); //EMAIL & NAME is case sensitive $this->csv_model->insert_csv($csv_array[$key]); //call model to insert data in DB } } else $data['error'] = "Error occured"; $this->load->view('csvupload_view', $data); } } } ?> 应该做的工作。这是@Cebri更一般的答案的特例。

  • (2*rand() - 1) / 100.0
  • rand(): [0, 1]
  • 2*rand(): [0, 2]
  • 2*rand()-1: [-1, 1]
相关问题