matlab学习总结

一、randn的用法
randn产生均值为0,方差 σ^2 = 1,标准差σ = 1的正态分布的随机数或矩阵的函数
用法:
Y = randn(n)
返回一个n*n的随机项的矩阵。如果n不是个数量,将返回错误信息。
Y = randn(m,n)  Y = randn([m n]) 
返回一个m*n的随机项矩阵。
Y = randn(m,n,p,...)  Y = randn([m n p...])
产生随机数组。
Y = randn(size(A)) 
返回一个和A有同样维数大小的随机数组。
randn
返回一个每次都变化的数量。
s = randn('state')
举例:

 1. R = randn(3,4) 将生成矩阵

R =
1.1650 0.3516 0.0591 0.8717
0.6268 -0.6965 1.7971 -1.4462
0.0751 1.6961 0.2641 -0.7012
For a histogram of the randn distribution, see hist. 

 2. 产生一个随机分布的指定均值和方差的矩阵:将randn产生的结果乘以标准差,然后加上期望均值即可。例如,产生均值为0.6,方差为0.1的一个5*5的随机数方式如下:

x = .6 + sqrt(0.1) * randn(5)
x =
0.8713 0.4735 0.8114 0.0927 0.7672
0.9966 0.8182 0.9766 0.6814 0.6694
0.0960 0.8579 0.2197 0.2659 0.3085
0.1443 0.8251 0.5937 1.0475 -0.0864
0.7806 1.0080 0.5504 0.3454 0.5813
其他类似函数:rand, randperm, sprand, sprandn
英文:
-------------------------------------------------------------
randn
Normally distributed random numbers and arrays 

Syntax
Y = randn(n)
Y = randn(m,n)
Y = randn([m n])
Y = randn(m,n,p,...)
Y = randn([m n p...])
Y = randn(size(A))
randn
s = randn('state')

Description
The randn function generates arrays of random numbers whose elements are normally distributed with mean 0, variance σ^2 = 1 , and standard deviation σ = 1. 

Y = randn(n) returns an n-by-n matrix of random entries. An error message appears if n is not a scalar. Y = randn(m,n) or Y = randn([m n]) returns an m-by-n matrix of random entries. Y = randn(m,n,p,...) or Y = randn([m n p...]) generates random arrays. Y = randn(size(A)) returns an array of random entries that is the same size as A. randn, by itself, returns a scalar whose value changes each time it's referenced. s = randn('state') returns a 2-element vector containing the current state of the normal generator. To change the state of the generator: randn('state',s)Resets the state to s. randn('state',0)Resets the generator to its initial state. randn('state',j)For integer j, resets the generator to its jth state. randn('state',sum(100*clock))Resets it to a different state each time.ExamplesExample 1. R = randn(3,4) may produce   R =
1.1650 0.3516 0.0591 0.8717
0.6268 -0.6965 1.7971 -1.4462
0.0751 1.6961 0.2641 -0.7012
For a histogram of the randn distribution, see hist. Example 2. Generate a random distribution with a specific mean and variance . To do this, multiply the output of randn by the standard deviation , and then add the desired mean. For example, to generate a 5-by-5 array of random numbers with a mean of .6 that are distributed with a variance of 0.1 x = .6 + sqrt(0.1) * randn(5)
x =
0.8713 0.4735 0.8114 0.0927 0.7672
0.9966 0.8182 0.9766 0.6814 0.6694
0.0960 0.8579 0.2197 0.2659 0.3085
0.1443 0.8251 0.5937 1.0475 -0.0864
0.7806 1.0080 0.5504 0.3454 0.5813
See Also rand, randperm, sprand, sprandn

二、Unbalanced or misused parentheses or brackets

翻译成汉语就是,不对称或误用的圆括号或括弧.

产生这种错误的可能原因有:

可能是语句赋值的时候没有带括号

只用了括弧一次,忘了第二次括号,即括号不对称

可能用了错误的括弧,中括或小括号

三、ans的含义

1.数值计算

MATLAB下进行基本数学运算,只需将运算式直接打入提示号(>>)之后,并按入Enter键即可。例如:  

>> (5*2+1.3-0.8)*10/25  

ans =4.2000  

MATLAB会将运算结果直接存入一变量ans,代表MATLAB运算后的答案(Answer)并显示其数值于幕上。

小提示: ">>"MATLAB的提示符号(Prompt),但在PC中文视窗系统下,由于编码方式不同,此提示符号常会消失不见,但这并不会影响到MATLAB的运算结果。  

我们也可将上述运算式的结果设定给另一个变量x  

x = (5*2+1.3-0.8)*10^2/25  

x = 42 

此时MATLAB会直接显示x的值。

2.显示没有设定变量名的输出量

四、M文件的命名规则

M文件的命名有一定规则。当不按规则命名时会使M文件不能正常运行。

规则一:文件名首字符不能是数字或下划线。

以一个简单的绘图程序为例。其M文件被存于F盘中,并以“111”作为文件名。

运行后,并没有生成图形,而在命令窗口(Command Window)中返回了数字文件名ans=111

改用“_111”为文件名。命令窗口就出现错误信息

使用“M111”为文件名。程序就正常运行。

如果用数字命名该M文件,程序只返回数字文件名,不作运行。

规则二:文件名不能与Matlab的内部函数名相同。M文件名的命名尽量不要是简单的英文单词,最好是由大小写英文/数字/下划线等组成。原因是简单的单词命名容易与Matlab内部函数同名。

规则三M文件名中不能有空格。若需要用两个或以上单词组合作为文件名,各单词必须直接连接组合(可把每个单词首字母大写以作区分或使用下划线)。

规则四M文件名中不能有汉字。

另外,对于前人总结的M文件命名规则中“M文件存储路径一定要是英文,作了测试。当选取好当前路径(Current Directory),M文件存储路径可以有中文。

 

      本文转自 gaochaojs 51CTO博客,原文链接:http://blog.51cto.com/jncumter/291582,如需转载请自行联系原作者



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值