Thursday, February 23, 2006
Random Generator
>> help spdiags
SPDIAGS Sparse matrix formed from diagonals.
SPDIAGS, which generalizes the function "diag", deals with three
matrices, in various combinations, as both input and output.
[B,d] = SPDIAGS(A) extracts all nonzero diagonals from the m-by-n
matrix A. B is a min(m,n)-by-p matrix whose columns are the p
nonzero diagonals of A. d is a vector of length p whose integer
components specify the diagonals in A.
B = SPDIAGS(A,d) extracts the diagonals specified by d.
A = SPDIAGS(B,d,A) replaces the diagonals of A specified by d with
the columns of B. The output is sparse.
A = SPDIAGS(B,d,m,n) creates an m-by-n sparse matrix from the
columns of B and places them along the diagonals specified by d.
Roughly, A, B and d are related by
for k = 1:p
B(:,k) = diag(A,d(k))
end
Example: These commands generate a sparse tridiagonal representation
of the classic second difference operator on n points.
e = ones(n,1);
A = spdiags([e -2*e e], -1:1, n, n)
Some elements of B, corresponding to positions "outside" of A, are
not actually used. They are not referenced when B is an input and
are set to zero when B is an output. If a column of B is longer than
the diagonal it's representing, elements of super-diagonals of A
correspond to the lower part of the column of B, while elements of
sub-diagonals of A correspond to the upper part of the column of B.
Example: This uses the top of the first column of B for the second
sub-diagonal and the bottom of the third column of B for the first
super-diagonal.
B = repmat((1:n)',1,3);
S = spdiags(B,[-2 0 1],n,n);
See also DIAG.
ode文件
ode文件是一种函数M文件,即:在m-file编辑器中,文件必须以function开头,后面跟的函数名必须与将来保存的M文件名一致。函数文件第一句的具体形式为:function y=average(x)
其中为y输出变量名,x为变量,average为函数名,保存时文件名必须是average.m。当函数有一个以上的输出函数时,输出参数包含在方括号里,变参包含在圆括号里,即函数文件的第一行的具体形式为:
function [out1,out2,…]=average(in1,in2,…)
例:计算数组元素的平均值
解:function y=average(x)
y=sum(x)/length(x) 注:上下两式等号前的符号"y"必须相同
為了要有隨機的數值,使用c內部的rand函式,是個不錯的選擇,但是以前沒有用過,所以重新來整理一下。
RAND_MAX: 是個非常大的數值,所以隨機函式庫所產生的數值,都會小、等於RAND_MAX。 rand(): 產生0至RAND_MAX的整數值。
所以(rand()+1.)/(RAND_MAX+2.)就會產生介於0與1的隨機實數。
Wednesday, February 22, 2006
conversion format
textread('Value.txt', 'N= %f\n', 1)
TEXTREAD Read formatted data from text file.
A = TEXTREAD('FILENAME')
A = TEXTREAD('FILENAME','',N)
A = TEXTREAD('FILENAME','',param,value, ...)
A = TEXTREAD('FILENAME','',N,param,value, ...) reads numeric data from
the file FILENAME into a single variable. If the file contains any
text data, an error is produced.
[A,B,C, ...] = TEXTREAD('FILENAME','FORMAT')
[A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N)
[A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',param,value, ...)
[A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N,param,value, ...) reads
data from the file FILENAME into the variables A,B,C,etc. The type of
each return argument is given by the FORMAT string. The number of
return arguments must match the number of conversion specifiers in the
FORMAT string. If there are fewer fields in the file than in the
format string, an error is produced. See FORMAT STRINGS below for
more information.
If N is specified, the format string is reused N times. If N is -1 (or
not specified) TEXTREAD reads the entire file.
If param,value pairs are supplied, user configurable options customize
the behavior of TEXTREAD. See USER CONFIGURABLE OPTIONS below.
TEXTREAD works by matching and converting groups of characters from the
file. An input field is defined as a string of non-whitespace
characters extending to the next whitespace or delimiter character
or until the field width is exhausted. Repeated delimiter characters
are significant while repeated whitespace characters are treated as
one.
FORMAT STRINGS
If the FORMAT string is empty, TEXTREAD will only numeric data.
The FORMAT string can contain whitespace characters (which are
ignored), ordinary characters (which are expected to match the next
non-whitespace character in the input), or conversion specifications.
If whitespace is set to '' and format types are %s,%q,%[...] and %[^...].
Else whitespace characters are ignored.
Supported conversion specifications:
%n - read a number - float or integer (returns double array)
%5n reads up to 5 digits or until next delimiter
%d - read a signed integer value (returns double array)
%5d reads up to 5 digits or until next delimiter
%u - read an integer value (returns double array)
%5u reads up to 5 digits or until next delimiter
%f - read a floating point value (returns double array)
%5f reads up to 5 digits or until next delimiter
%s - read a whitespace separated string (returns cellstr)
%5s reads up to 5 characters or until whitespace
%q - read a (possibly double quoted) string (returns cellstr)
%5q reads up to 5 non-quote characters or until whitespace
%c - read character or whitespace (returns char array)
%5c reads up to 5 characters including whitespace
%[...] - reads characters matching characters between the
brackets until first non-matching character or
whitespace (returns cellstr)
use %[]...] to include ]
%5[...] reads up to 5 characters
%[^...] - reads characters not matching characters between the
brackets until first matching character or whitespace
(returns cellstr)
use %[^]...] to exclude ]
%5[^...] reads up to 5 characters
Note: Format strings are interpreted as with sprintf before parsing.
For example, textread('mydata.dat','%s\t') will search for a tab not
the character '\' followed by the character 't'. See the Language
Reference Guide or a C manual for complete details.
Using %* instead of % in a conversion causes TEXTREAD to skip the
matching characters in the input (and no output is created for this
conversion). The % can be followed by an optional field width to
handle fixed width fields. For example %5d reads a 5 digit integer. In
addition the %f format supports the form %.f.
USER CONFIGURABLE OPTIONS
Possible param/value options are:
'bufsize' - maximum string length in bytes (default is 4095)
'commentstyle' - one of
'matlab' -- characters after % are ignored
'shell' -- characters after # are ignored
'c' -- characters between /* and */ are ignored
'c++' -- characters after // are ignored
'delimiter' - delimiter characters (default is none)
'emptyvalue' - empty cell value in delimited files (default is 0)
'endofline' - end of line character (default determined from file)
'expchars' - exponent characters (default is 'eEdD')
'headerlines' - number of lines at beginning of file to skip
'whitespace' - whitespace characters (default is ' \b\t')
TEXTREAD is useful for reading text files with a known format. Both
fixed and free format files can be handled.
Examples:
Suppose the text file mydata.dat contains data in the following form:
Sally Type1 12.34 45 Yes
Joe Type2 23.54 60 No
Bill Type1 34.90 12 No
Read each column into a variable
[names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');
Read first column into a cell array (skipping rest of line)
[names]=textread('mydata.dat','%s%*[^\n]')
Read first character into char array (skipping rest of line)
[initials]=textread('mydata.dat','%c%*[^\n]')
Read file as a fixed format file while skipping the doubles
[names,types,y,answer] = textread('mydata.dat','%9c%5s%*f%2d%3s');
Read file and match Type literal
[names,typenum,x,y,answer]=textread('mydata.dat','%sType%d%f%d%s');
Read m-file into cell array of strings
file = textread('fft.m','%s','delimiter','\n','whitespace','');
To read all numeric data from a delimited text file, use a single output
argument, empty format string, and the appropriate delimiter. For
example, suppose data.csv contains:
1,2,3,4
5,6,7,8
9,10,11,12
Read the whole matrix into a single variable:
[data] = textread('data.csv','','delimiter',',');
Read the first two columns into two variables:
[col1, col2] = textread('data.csv','%n%n%*[^\n]','delimiter',',');
For files with empty cells, use the emptyvalue parameter. Suppose
data.csv contains:
1,2,3,4,,6
7,8,9,,11,12
Read the file like this, using NaN in empty cells:
[data] = textread('data.csv','','delimiter',',','emptyvalue',NaN);
See also DLMREAD, LOAD, STRREAD, SSCANF, XLSREAD.
d, i, o, u, x, e, f, g, s, c,
>> help fscanf
FSCANF Read formatted data from file.
[A,COUNT] = FSCANF(FID,FORMAT,SIZE) reads data from the file specified
by file identifier FID, converts it according to the specified FORMAT
string, and returns it in matrix A. COUNT is an optional output
argument that returns the number of elements successfully read.
FID is an integer file identifier obtained from FOPEN.
SIZE is optional; it puts a limit on the number of elements that
can be read from the file; if not specified, the entire file
is considered; if specified, valid entries are:
N read at most N elements into a column vector.
inf read at most to the end of the file.
[M,N] read at most M * N elements filling at least an
M-by-N matrix, in column order. N can be inf, but not M.
If the matrix A results from using character conversions only and
SIZE is not of the form [M,N] then a row vector is returned.
FORMAT is a string containing C language conversion specifications.
Conversion specifications involve the character %, optional
assignment-suppressing asterisk and width field, and conversion
characters d, i, o, u, x, e, f, g, s, c, and [. . .] (scanset).
Complete ANSI C support for these conversion characters is
provided consistent with 'expected' MATLAB behavior. For a complete
conversion character specification, see the Language Reference
Guide or a C manual.
If %s is used an element read may cause several MATLAB matrix
elements to be used, each holding one character. Use %c to read
space characters; the format %s skips all white space.
Mixing character and numeric conversion specifications will cause
the resulting matrix to be numeric and any characters read to show
up as their ASCII values one character per MATLAB matrix element.
FSCANF differs from its C language namesake in an important respect -
it is "vectorized" in order to return a matrix argument. The format
string is recycled through the file until an end-of-file is reached
or the amount of data specified by SIZE is read in.
Examples:
S = fscanf(fid,'%s') reads (and returns) a character string.
A = fscanf(fid,'%5d') reads 5-digit decimal integers.
See also FPRINTF, SSCANF, TEXTREAD, FGETL, FGETS, FREAD, INPUT.Wednesday, February 08, 2006
Matlab Syntax Usage
Logical Operator
Operator Operation Priority ~ NOT Highest & Elementwise AND | Elementwise OR && Short-circuit AND || Short-circuit OR Lowest Logical operators, short-circuit, &&, || Relational operators <, <=, >, >=, ==, ~=
Friday, February 03, 2006
Recursive Function
Recursive Function in Matlab
f = [1 1]; n = 1; while f(n) + f(n + 1) < 80 f(n + 2) = f(n) + f(n + 1); n = n + 1; end f function f = fibfun(n) % Incidentally, the name Fibonacci comes from Filius Bonassi, or "son of Bonassus". if n > 2 f = fibfun(n - 1) + fibfun(n - 2); else f = 1; end end
Thursday, February 02, 2006
OOP@Matlab
Matlab也可以物件導向設計, OO@Matlab,但是寫法不像C++, Python那樣地straightforward。