收藏 分销(赏)

MATLAB函数和命令的用法.doc

上传人:二*** 文档编号:4514311 上传时间:2024-09-26 格式:DOC 页数:22 大小:524KB 下载积分:5 金币
下载 相关 举报
MATLAB函数和命令的用法.doc_第1页
第1页 / 共22页
本文档共22页,全文阅读请下载到手机保存,查看更方便
资源描述
Binocdf二项式累积分布函数 语法格式 Y = binocdf(X,N,P) 函数功能 Y = binocdf(X,N,P) 计算X中每个X(i)的二项式累积分布函数,其中,N中对应的N(i)为试验数,P中对应的P(i)为每次试验成功的概率。Y, N, 和 P 的大小类型相同,可以是向量、矩阵或多维数组。输入的标量将扩展成一个数组,使其大小类型与其它输入相一致。 The values in N must all be positive integers, the values in X must lie on the interval [0,N], and the values in P must lie on the interval [0, 1]. The binomial cdf for a given value x and a given pair of parameters n and p is The result, y, is the probability of observing up to x successes in n independent trials, where the probability of success in any given trial is p. The indicator function I(0,1,...,n)(i)ensures that x only adopts values of 0,1,...,n. 示例 若一个棒球队在一个赛季要比赛162场,每场比赛取胜的机会是50-50,则该队取胜超过100 场的概率为: >> 1-binocdf(100,162,0.5) ans = 0.0010433 相关函数 binofit | binoinv | binopdf | binornd | binostat | cdf 附:二项式分布(binomial distribution ) 定义 二项分布的概率密度函数为 where k is the number of successes in n trials of a Bernoulli process with probability of success p. The binomial distribution is discrete, defined for integers k = 0, 1, 2, ... n, where it is nonzero. 背景 The binomial distribution models the total number of successes in repeated trials from an infinite population under the following conditions: Only two outcomes are possible on each of n trials. The probability of success for each trial is constant. All trials are independent of each other. The binomial distribution is a generalization of the Bernoulli distribution; it generalizes to the multinomial distribution. 参数 Suppose you are collecting data from a widget manufacturing process, and you record the number of widgets within specification in each batch of 100. You might be interested in the probability that an individual widget is within specification. Parameter estimation is the process of determining the parameter, p, of the binomial distribution that fits this data best in some sense. One popular criterion of goodness is to maximize the likelihood function. The likelihood has the same form as the binomial pdf above. But for the pdf, the parameters (n and p) are known constants and the variable is x. The likelihood function reverses the roles of the variables. Here, the sample values (the x's) are already observed. So they are the fixed constants. The variables are the unknown parameters. MLE involves calculating the value of p that give the highest likelihood given the particular set of data. The function binofit returns the MLEs and confidence intervals for the parameters of the binomial distribution. Here is an example using random numbers from the binomial distribution with n = 100 and p = 0.9. >> r = binornd(100,0.9) r = 85 >> [phat, pci] = binofit(r,100) phat = 0.85 pci = 0.76469 0.91355 The MLE for parameter p is 0.8800, compared to the true value of 0.9. The 95% confidence interval for p goes from 0.7998 to 0.9364, which includes the true value. In this made-up example you know the "true value" of p. In experimentation you do not. 示例 The following commands generate a plot of the binomial pdf for n = 10 and p = 1/2. x = 0:10; y = binopdf(x,10,0.5); plot(x,y,'+') 相关内容 Discrete Distributions 附:二项式分布(网上) 定义 若某事件概率为p,现重复试验n次,该事件发生k次的概率为: P=C(k,n)pk(1-p)(n-k) C(k,n)表示组合数,即从n个事物中拿出k个的方法数。 二项分布的概念 考虑只有两种可能结果的随机试验,当成功的概率(π)是恒定的,且各次试验相互独立,这种试验在统计学上称为贝努里试验(Bernoullitrial)。 如果进行n次贝努里试验,取得成功次数为X(X=0,1,…,n)的概率可用下面的二项分布概率公式来描述: P=C(X,n) πX(1-π)(n-X) 式中的n为独立的贝努里试验次数,π为成功的概率,(1-π)为失败的概率,X为在n次贝努里试验中出现成功的次数,C(X,n)表示在n次试验中出现X的各种组合情况,在此称为二项系数(binomialcoefficient)。 内容简介 二项分布,伯努里分布:进行一系列试验,如果 1. 在每次试验中只有两种可能的结果,而且是互相对立的; 2. 每次实验是独立的,与其它各次试验结果无关; 3. 结果事件发生的概率在整个系列试验中保持不变,则这一系列试验称为伯努力试验。 在这试验中,事件发生的次数为一随机事件,它服从二次分布。二项分布可以用于可靠性试验。可靠性试验常常是投入n个相同的式样进行试验T小时,而只允许k个式样失败,应用二项分布可以得到通过试验的概率。 一个事件必然出现,就说它100%要出现。100%=1,所以100%出现的含义就是出现的概率P=1。即必然事件的出现概率为1。 若掷一枚硬币,正面向上的结果的概率为0.5。反面向上的结果的概率也是0.5。则出现正面向上事件或者反面向上事件的概率就是0.5+0.5=1,即二者必居其一。 若掷两次硬币,由独立事件的概率乘法定理那么两次都是正面(反面)向上的概率是0.5×0.5=0.25。另外第一个是正第二个是反的出现概率也是0.5×0.5=0.25。同理第一个反第二个正的出现概率也是0.5×0.5=0.25。于是一正一反的概率是前面两个情况的和,即0.25+0.25=2×0.25=0.5。它们的合计值仍然是1。 binopdf二项分布的概率密度函数 语法格式 Y = binopdf(X,N,P) 函数功能 Y = binopdf(X,N,P) 计算X中每个X(i)的概率密度函数,其中,N中对应的N(i)为试验数,P中对应的P(i)为每次试验成功的概率。Y, N, 和 P 的大小类型相同,可以是向量、矩阵或多维数组。输入的标量将扩展成一个数组,使其大小类型与其它输入相一致。 N中的值必须是正整数,0 ≤ P ≤ 1 。 对于给出的x和两个参数n和p,二项分布概率密度函数为 其中 q = 1 – p。 y为n次独立试验中成功x次的概率,其中,每次试验成功的概率为p。指示器函数 I(0,1,...,n)(x) 确保x 取值为 0, 1, ..., n。 示例 一个质量检查技术员一天能测试200块电路板。假设有 2% 的电路板有缺陷,该技术员在一天的测试中没有发现有缺陷的电路板的概率是多少? >>binopdf(0,200,0.02) ans = 0.0176 质量检查技术员一天中最大可能检测出有缺陷的电路板是多少块? >>defects=0:200; >>y = binopdf(defects,200,.02); >>[x,i]=max(y); >>defects(i) ans = 4 相关函数 binocdf | binofit | binoinv | binornd | binostat | pdf dlmread将以ASCII码分隔的数值数据文件读入到矩阵中 语法格式 M = dlmread(filename) M = dlmread(filename, delimiter) M = dlmread(filename, delimiter, R, C) M = dlmread(filename, delimiter, range) 函数功能 M = dlmread(filename) 读取有分隔符的ASCII数值数据文件filename,并把数据给到矩阵M中。文件名(filename)以字符串形式用单引号括起来。 dlmread 从文件的格式中推断分隔符。 M = dlmread(filename, delimiter) 指定分隔符delimiter。使用 '\t' 代表制表符 tab 作为分隔。 M = dlmread(filename, delimiter, R, C) R和C指定了数据 读取数据,其左上角的值位于文件中的第R行,第C列。R和C从0开始,所以R=0, C=0 指向文件中的第一个值。 M = dlmread(filename, delimiter, range) 读取由range = [R1 C1 R2 C2]指定的区域块, (R1,C1)是左上角,(R2,C2)是右下角。也可以使用电子表格表示法来指定,如range = 'A1..B7'。 备注 Ÿ All data in the input file must be numeric. dlmread does not read files that contain nonnumeric data, even if the specified rows and columns contain only numeric data. Ÿ 若没有指定分隔符,当从文件格式中推断分隔符时,连续的空格符当作一个分隔符对待。若指定了分隔符,则重复的分隔符将分别作为单独的分隔符对待。 Ÿ If you want to specify an R, C, or range input, but not a delimiter, set the delimiter argument to the empty string, (two consecutive single quotes with no spaces in between, ''). For example, M = dlmread('myfile.dat', '', 5, 2) In this case, dlmread treats repeated white spaces as a single delimiter. Ÿ Dlmread将用0填充没有边界的区域。有多行的数据文件,若以非空格分隔符结束,例如分号,则导入后会多产生全0的一列在最后。 Ÿ Dlmread在导入任何复数时,将其作为一个整体导入到一个复数单元中。下面是有效的复数格式: ±<real>±<imag>i|j Example: 5.7-3.1i ±<imag>i|j Example: -7j 嵌入了空格的复数是不正确的格式,空格将被认为是分隔符。 示例 例1 Export a 5-by-8 test matrix M to a file, and read it with dlmread, first with no arguments other than the filename: M = gallery('integerdata', 100, [5 8], 0); dlmwrite('myfile.txt', M, 'delimiter', '\t') dlmread('myfile.txt') ans = 96 77 62 41 6 21 2 42 24 46 80 94 36 20 75 85 61 2 93 92 82 61 45 53 49 83 74 42 1 28 94 21 90 45 18 90 14 20 47 68 Now read a portion of the matrix by specifying the row and column of the upper left corner: dlmread('myfile.txt', '\t', 2, 3) ans = 92 82 61 45 53 42 1 28 94 21 90 14 20 47 68 This time, read a different part of the matrix using a range specifier: dlmread('myfile.txt', '\t', 'C1..G4') ans = 62 41 6 21 2 80 94 36 20 75 93 92 82 61 45 74 42 1 28 94 例2 Export matrix M to a file, and then append an additional matrix to the file that is offset one row below the first: M = magic(3); dlmwrite('myfile.txt', [M*5 M/5], ' ') dlmwrite('myfile.txt', [M/3], '-append', ... 'roffset', 1, 'delimiter', ' ') type myfile.txt 40 5 30 1.6 0.2 1.2 15 25 35 0.6 1 1.4 20 45 10 0.8 1.8 0.4 2.6667 0.33333 2 1 1.6667 2.3333 1.3333 3 0.66667 When dlmread imports these two matrices from the file, it pads the smaller matrix with zeros: dlmread('myfile.txt') 40.0000 5.0000 30.0000 1.6000 0.2000 1.2000 15.0000 25.0000 35.0000 0.6000 1.0000 1.4000 20.0000 45.0000 10.0000 0.8000 1.8000 0.4000 2.6667 0.3333 2.0000 0 0 0 1.0000 1.6667 2.3333 0 0 0 1.3333 3.0000 0.6667 0 0 0 替代 作为dlmread的替代,可使用导入向导。选择菜单 “File | Import Data” 激活导入向导。 相关函数 dlmwrite | textscan fprintf写数据到文本文件或输出到命令窗口 语法格式 fprintf(fileID, format, A, ...) fprintf(format, A, ...) count = fprintf(...) 函数功能 fprintf(fileID, format, A, ...) fileID为文件句柄,指定要写入的文件;format是用来控制所写数据格式的格式符;A是用来存放数据的矩阵。 applies the format to all elements of array A and any additional array arguments in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen. fprintf(format, A, ...) 将A中的数据按格式format输出到命令窗口。 count = fprintf(...) 返回fprintf写的字节数。 输入参数 fileID 取值为下面中的一种: ² 从文件打开时所得到的文件句柄,为一个整数值。 ² 1 为标准输出设备(屏幕)。 ² 2 for standard error. fileID缺省时,输出到屏幕,即1为默认值。 format 单引号括起来的是 “转换控制字符串 ”,指定输出的格式。由下面给出的各部分的组合而成: ² 百分号后紧跟的格式字符,如'%s' 。 ² 输出区域的宽度,精度,和其它选项(附加格式说明符)。 ² 要原样输出的文本(普通字符)。 ² 转义字符,包括: '' 单引号 %% 百分号 \\ 反斜线 \a 响铃 \b 退格符 \f Form feed \n 换行 \r 回车 \t 水平制表符 \v 垂直制表符 \xN 十六进制数, N \N 八进制数, N 转换字符和可选的操作符按以下顺序排列(包括空格): 下表列出可用的转换字符和限定类型字符。 数据类型 转换 说 明 Integer, signed %d 或 %i 十进制整数 %ld 或 %li 64-bit base 10 values %hd 或 %hi 16-bit base 10 values Integer, unsigned %u 十进制整数 %o 八进制整数(octal) %x 十六进制整数 (hexadecimal), 使用字母 a–f %X 十六进制整数 (hexadecimal), 使用字母A–F %lu %lo %lx or %lX 64-bit values, 十、八、十六进制整数 %hu %ho %hx or %hX 16-bit values, 十、八、十六进制整数 Floating-point number %f 定点表示法 %e 指数表示法,如 3.141593e+00 %E 同 %e,但用E,如 3.141593E+00 %g %e 或 %f的紧凑格式,没有尾部的0 %G %E 或 %f的紧凑格式,没有尾部的0 %bx or %bX %bo %bu 双精度的十六、八、十进制数值 如:%bx prints pi as 400921fb54442d18 %tx or %tX %to %tu 单精度的十六、八、十进制数值 如:%tx prints pi as 40490fdb Characters %c 单个字符 %s 字符串 附加格式说明符,包括: ² 字段宽度 Minimum number of characters to print. Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%12d', intmax) is equivalent to ('%*d', 12, intmax). ² 精度 For %f, %e, or %E: Number of digits to the right of the decimal point. Example: '%6.4f' prints pi as '3.1416' For %g or %G Number of significant digits. Example: '%6.4g' prints pi as ' 3.142' Can be a number, or an asterisk (*) to refer to an argument in the input list. For example, the input list ('%6.4f', pi) is equivalent to ('%*.*f', 6, 4, pi). ² Flags Action Flag Example Left-justify. '–' %-5.2f Print sign character (+ or –). '+' %+5.2f Insert a space before the value. ' ' % 5.2f Pad with zeros. '0' %05.2f Modify selected numeric conversions: ² For %o, %x, or %X, print 0, 0x, or 0X prefix. ² For %f, %e, or %E, print decimal point even when precision is 0. ² For %g or %G, do not remove trailing zeros or decimal point. '#' %#5.0f ² Identifier Order for processing inputs. Use the syntax n$, where n represents the position of the value in the input list. For example, '%3$s %2$s %1$s %2$s' prints inputs 'A', 'B', 'C' as follows: C B A B. The following limitations apply to conversions: ² Numeric conversions print only the real component of complex numbers. ² If you apply an integer or string conversion to a numeric value that contains a fraction, MATLAB overrides the specified conversion, and uses %e. ² If you apply a string conversion (%s) to integer values, MATLAB converts values that correspond to valid character codes to characters. For example, '%s' converts [65 66 67] to ABC. ² Different platforms display exponential notation (such as %e) with a different number of digits in the exponent. 环境 示例 Windows 1.23e+004 UNIX 1.23e+04 ² 不同的操作系统环境显示的负零(-0) 不相同。   转换字符 环境 %e 或 %E %f %g 或 %G Windows 0.000000e+000 0.000000 0 Others -0.000000e+00 -0.000000 -0 A 数值或字符数组。 示例 例1 输出多个数值和文本到屏幕。 >> B = [8.8 7.7 ; 8800 7700]; >> fprintf('X is %4.2f meters or %8.3f mm\n', 9.9, 9900, B) X is 9.90 meters or 9900.000 mm X is 8.80 meters or 8800.000 mm X is 7.70 meters or 7700.000 mm 注:对B是按列处理。 例2 对双精度数四舍五入取整,并输出到屏幕。 >> a = [1.02 3.04 5.06]; >> fprintf('%d\n', round(a)); 1 3 5 例3 写一组数据到文本文件中。 x = 0:.1:1; y = [x; exp(x)]; % 打开文件exp.txt为可写 fid = fopen('exp.txt', 'w'); fprintf(fid, '%6.2f %12.8f\n', y); fclose(fid); % 查看文件内容 type exp.txt MATLAB导入功能,所有UNIX应用程序,和微软Word和写字板都把' \ n '作为一个换行符指示器。然而,如果你要用微软记事本读取文件,请使用“\ r \ n”移动到新行再写。将之前的调用改写如下: fprintf(fid, '%6.2f %12.8f\r\n', y); 输出结果不变。 例4 在Windows系统上,将PC形式的指数表示法(三位数的指数)转换到unix形式的表示法(两位数字),并打印数据到一个文件: a = [0.06 0.1 5 300] % 用sprintf转换数值数据到文本,使用%e a_str = sprintf('%e\t',a) % use strrep to replace exponent prefix with shorter version a_str = strrep(a_str,'e+0','e+'); a_str = strrep(a_str,'e-0','e-'); %调用fprintf打印更新的文本字符串 fid = fopen('newfile.txt','w'); fprintf(fid, '%s', a_str); fclose(fid); % 查看文件内容 type newfile.txt 例5 在屏幕上显示一个超链接。 例6 创建一个字符矩阵并存入磁盘,再读出赋值给另一个矩阵。 >> a='string'; >> fid=fopen('d:\char1.txt','w'); >> fprintf(fid,'%s',a); >> fclose(fid); >> fid1=fopen('d:\char1.txt','rt'); >> b=fscanf(fid1,'%s') b = string 例7 用fprintf输出一个数值矩阵。 i = 1:10; square_root = sqrt(i); square = i.^2; cube = i.^3; % 拼接成一个大矩阵,行向量转置成列向量 out = [ i' square_root' square' cube' ]; % 输出数据 for i = 1:10 fprintf (' %2d %11.4f %6d %8d\n',out(i,:)); end%第1列整数输出,占2格 %第2列实数输出,占11格,4位小数 i = 1:10; square_root = sqrt(i); square = i.^2; cube = i.^3; % 拼接成一个大矩阵 out = [ i; square_root; square; cube]; % 输出数据 fprintf (' %2d %11.4f %6d %8d\n',out); %将第1列输出为第1行,格式对应; %将第2列输出为第2行,格式对应;… 参考文献 [1] Kernighan, B. W., and D. M. Ritchie, The C Programming Language, Second Edition, Prentice-Hall, Inc., 1988. [2] ANSI specification X3.159-1989: "Programming Language C," ANSI, 1430 Broadway, New York, NY 10018. 相关函数 disp | fclose | ferror | fopen | fread | fscanf | fseek | ftell | fwrite | sprintf num2str将数字转换为字符串 语法格式(MATLAB帮助) str = num2str(A) str = num2str(A, precision) str = num2str(A, format) 函数功能 num2str 函数将数字转换为其字符串表示形式。该函数在用数值来标注画图时很有用。 str = num2str(A) 将数组A转换为其字符串表示形式并存入 str。浮点数将转换为最多5位有效数字的指数形式。整数将转换为精确值的字符串表示形式。 str = num2str(A, precision) 将数组A转换为其字符串表示形式并存入 str,最多有效数字个数由precision给定。 str = num2str(A, format) converts array A using the supplied format, as described on the fprintf function reference page. By default, num2str displays floating point values using the '%11.4g' format (four significant digits in exponential or fixed-point notation, whichever is shorter). 备注 与fprintf不同, num2str函数trims any leading spaces from a string, even when used with the space character flag. 示例 使用格式 '%10.5e\n' 的num2str函数返回一个指数格式的字符矩阵,有5位小数,每个数据用新行字符'\n'分隔。 相关函数 cast, fprintf, int2str, mat2str, sprintf, str2num 简介(网上) 函数名称: num2str 在matlab中,无论是内建函数还是工具箱函数, 2很常见, 这可能是因为2英文two和to发音相同。而2写起来也比较简单。 所以很多转换类函数都用2来命名而非to。比如number to string, 不是命名为numTostr而num2str。 函数功能: 把数值转换成字符串, 转换后可以使用fprintf或disp函数进行输出。在matlab命令窗口中键入doc num2str或help num2str即可获得该函数的帮助信息。 语法格式: str = num2str(A) 把数组A中的数转换成字符串表示形式。 str = num2str(A, precision) 把数组A转换成字符串形式表示,precision表示精度。比如precision为3表示保留最多3位有效数字 >> num2str(0.5345,3) ans = 0.534 >> num2str(1.2345,3) ans = 1.23 即从左边第一个不为0的数字开始保留3个数字。 str = num2
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服