求马氏距离_matlab解法
- 格式:doc
- 大小:46.50 KB
- 文档页数:7
求马氏距离(Mahalanobis distance )--matlab版方法一:X = [1 2; 1 3; 2 2; 3 1];[mx,nx] = size(X);Dis = ones(mx,nx);Cov = cov(X);for i=1:mxfor j=1:nxD(i,j)=((X(i,:)-X(j,:))*inv(C)*(X(i,:)-X(j,:))')^0.5;endendD>> XX =1 21 32 23 1>> DD =0 2.3452 2.0000 2.34522.3452 0 1.2247 2.44952.0000 1.2247 0 1.22472.3452 2.4495 1.2247 0>> 2.3452 X的第一行向量与第二行向量之间的马氏距离。
2.0000 X的第一行向量与第三行向量之间的马氏距离。
方法二:X = [1 2; 1 3; 2 2; 3 1]X =1 21 32 23 1Y = pdist(X,'mahal')Y =2.3452 2.0000 2.3452 1.2247 2.4495 1.2247function d = mahalanobis(X, Mu, C)%MAHALANOBIS Mahalanobis distance.% D = MAHALANOBIS(X, MU, C) returns the Mahalanobis distance between % the length p vectors X and MU given the p by p covariance matrix % C. If omitted, it is assumed that C is the identity matrix(单位矩阵/恒等矩阵)% EYE(p). If either X or MU is an n by p matrix, D will be returned % as an n by g matrix where n is the number of rows in X and g is % the number of rows in MU where each entry i, j corresponds to the % mahalanobis distance between row i of X and row j of MU. If MU is % simply 0, it is treated as the origin from which Mahalanobis% distance to X is calculated. C must be a positive, definite,% symmetric matrix.%% The Mahalanobis distance between vectors X(i,:) and MU(j,:) is% defined as:%% D(i,j) = ((X(i,:) - MU(j,:))'*INV(C)*(X(i,:) - MU(j,:))).^(1/2)% Copyright (c) 1999 Michael Kiefte.% $Log$error(nargchk(2, 3, nargin))if isempty(X) | ~isa(X, 'double') | ~isreal(X) | ...any(any(isnan(X) | isinf(X)))error(['X must be a vector or matrix of real, finite numeric' ...' doubles.'])elseif length(X) == prod(size(X))X = X(:)';elseif ndims(X) ~= 2error('If X is a matrix, it must be a 2-d array.')end[n p] = size(X);if isempty(Mu) | ~isa(Mu, 'double') | ~isreal(Mu) | ...any(any(isnan(Mu) | isinf(Mu)))error(['Mu must be a vector or matrix of real, finite numeric' ...' doubles.'])elseif length(Mu) == prod(size(Mu))Mu = Mu(:)';elseif ndims(Mu) ~= 2error('If MU is a matrix, it must be a 2-d array.')endif length(Mu) == 1 & Mu == 0Mu = zeros(1, p);elseif size(Mu, 2) ~= perror('Number of features in MU and X must be the same.')endg = size(Mu, 1);if ~isempty(C)if ~isa(C, 'double') | ~isreal(C) | ndims(C) ~= 2 | ...any(any(isnan(C) | isinf(C)))error('C must be a matrix of real, finite numeric doubles.')elseif any(size(C) ~= p)error(['Number of rows and columns in C must match number of' ...' features in X and MU.'])endtryS = inv(chol(C));catcherror(['Covariance matrix C must be positive, definite,' ...' symmetric.'])endXs = X*S;Ms = Mu*S;elseXs = X;Ms = Mu;endd = sqrt(repmat(sum(Xs.^2, 2), 1, g) - 2*Xs*Ms' + ...repmat(sum(Ms'.^2), n, 1));spss快速聚类具体操作方法外部数据导入SPSS中2008年10月08日星期三21:09SPSS可以直接读入许多格式的数据文件,选择菜单File==>Open==>Data或直接单击快捷工具栏上的图标,系统就会弹出Open File对话框,单击“文件类型”列表框,在里面能看到直接打开的数据文件格式,分别是:* SPSS(*.sav):SPSS数据文件(6.0~10.0版);* SPSS/PC+(*.sys):SPSS 4.0版数据文件;* Systat(*.syd):*.syd格式的Systat 数据文件;* Systat(*.sys):*.sys格式的Systat 数据文件;* SPSS portable(*.por):SPSS便携格式的数据文件;* EXCEL(*.xls):EXCEL数据文件(从5.0版~2000版);* Lotus(*.w*):Lotus数据文件;* SYLK(*.slk):SYLK数据文件;* dBase(*.dbf):dBase系列数据文件(从dBase II~IV);* Text(*.txt):纯文本格式的数据文件;* data(*.dat):纯文本格式的数据文件。
在常用的其他类型的数据文件中,比较常用的是Text纯文本格式的数据文件(*.txt)与EXCEL 数据文件(*.xls)。
(注释:有的EXCEL文件也可转化为*.txt文件类型,再导入SPSS中)1. 纯文本格式的数据文件(*.txt)的转换由于(*.txt)纯文本的数据文件转换比较常见,因此在File的命令菜单就直接具有Read Text Data的命令项。
选择菜单File=>Read Text Data,系统就会弹出Open File对话框,并自动指向(*.txt)文件或者自己选择目录,打开(*.txt)文件。
操作如下:* File=>Read text data,进入Open File对话框,浏览并找到需要转换的文件“*.txt”,选中该文件,双击文件(或点击“打开”按钮),进入格式转换界面。
* “Step 1 of 6”说明该向导共分6步,这是第一步。
中间的“Text File:”后面指明了引用文本的路径,无需定义。
“Does your file match a predefined format”询问“文本文件是否和预定格式相一致?”。
默认是前者。
* 选择“No”以后,可以点击“取消”停止操作,也可以单击“帮助”寻求帮助信息。
如果没有问题,则可以点击“下一步”。
* “Step 2 of 6”最上方的问题是“How are your variables arranged?”,询问了变量的安排。
“Delimited”表示用Spaces(空格)、commas(逗号)、tabs(跳格键)及其他字符间隔开来。
“Fixed”表示变量与观测值都具有固定排列的位置,没有字符的间隔。
一般情况下文本的数据文件都是字符或空格间隔开来的,因此,通常选择“Delimited”。
“Are variable names included at the top of your file?”则询问了变量行是否位于文本文件的首行。
如果首行的变量名不适于SPSS软件的规定,系统将进行自动的定义。
* “Step 3 of 6”主要是对观测值进行界定。
“The first case of data begins on which line number? ”询问“第一个记录起始于哪一行?”。
如果第一行是变量名,这里就要选择2。
空白处的数字可以根据具体情况自由选择。
* “How are your cases represented?”询问“记录是如何存储于文本文件中的?”。
一般是“Each line represents a case”,表示“每一行代表一条记录”。
如果所需描述的变量较多、文本文件要用大于一行的长度来写完一条记录的特征量情况下,则是“A specific number of variables represents a case”,表示“每n行代表一条记录”,在空格中可以根据具体需要调整行数。