# 【Matlab开发】matlab中bar绘图设置与各种距离度量

## Matlab Bar图如何为每个bar设置不同颜色

data = [3, 7, 5, 2;4, 3, 2, 9;6, 6, 1, 4];
b = bar(data);


data = [3, 7, 5, 2;4, 3, 2, 9;6, 6, 1, 4];
b = bar(data);
ch = get(b,'children');
set(ch{1},'FaceVertexCData',[1;1;1;1;2;2;2;2;3;3;3;3;4;4;4;4])
set(ch{2},'FaceVertexCData',[1;1;1;1;2;2;2;2;3;3;3;3;4;4;4;4])
set(ch{3},'FaceVertexCData',[1;1;1;1;2;2;2;2;3;3;3;3;4;4;4;4])
set(ch{4},'FaceVertexCData',[1;1;1;1;2;2;2;2;3;3;3;3;4;4;4;4])


data = [3, 7, 5, 2];
b = bar(data);
ch = get(b,'children');
set(ch,'FaceVertexCData',[0 0 1;0 1 1;1 1 1;1 0 1;])


## matlab中用于求取各种距离的函数pdist方法的使用

### 引言说明

(1) d(x,x) = 0 // 到自己的距离为0
(2) d(x,y) >= 0 // 距离非负
(3) d(x,y) = d(y,x) // 对称性: 如果 A 到 B 距离是a，那么 B 到 A 的距离也应该是 a
(4) d(x,k)+ d(k,y) >= d(x,y) // 三角形法则:(两边之和大于第三边)

### matlab中用于求取各种距离的函数pdist方法的使用

help pdist
pdist Pairwise distance between observations.
D = pdist(X) returns a vector D containing the Euclidean distances
between each pair of observations in the M-by-N data matrix X. Rows of
X correspond to observations, columns correspond to variables. D is a
1-by-(M*(M-1)/2) row vector, corresponding to the M*(M-1)/2 pairs of
observations in X.

D = pdist(X, DISTANCE) computes D using DISTANCE.  Choices are:

'euclidean'   - Euclidean distance (default)
'seuclidean'  - Standardized Euclidean distance. Each coordinate
difference between rows in X is scaled by dividing
by the corresponding element of the standard
deviation S=NANSTD(X). To specify another value for
S, use D=pdist(X,'seuclidean',S).
'cityblock'   - City Block distance
'minkowski'   - Minkowski distance. The default exponent is 2. To
specify a different exponent, use
D = pdist(X,'minkowski',P), where the exponent P is
a scalar positive value.
'chebychev'   - Chebychev distance (maximum coordinate difference)
'mahalanobis' - Mahalanobis distance, using the sample covariance
of X as computed by NANCOV. To compute the distance
with a different covariance, use
D =  pdist(X,'mahalanobis',C), where the matrix C
is symmetric and positive definite.
'cosine'      - One minus the cosine of the included angle
between observations (treated as vectors)
'correlation' - One minus the sample linear correlation between
observations (treated as sequences of values).
'spearman'    - One minus the sample Spearman's rank correlation
between observations (treated as sequences of values).
'hamming'     - Hamming distance, percentage of coordinates
that differ
'jaccard'     - One minus the Jaccard coefficient, the
percentage of nonzero coordinates that differ
function      - A distance function specified using @, for
example @DISTFUN.

A distance function must be of the form

function D2 = DISTFUN(XI, XJ),

taking as arguments a 1-by-N vector XI containing a single row of X, an
M2-by-N matrix XJ containing multiple rows of X, and returning an
M2-by-1 vector of distances D2, whose Jth element is the distance
between the observations XI and XJ(J,:).

The output D is arranged in the order of ((2,1),(3,1),..., (M,1),
(3,2),...(M,2),.....(M,M-1)), i.e. the lower left triangle of the full
M-by-M distance matrix in column order.  To get the distance between
the Ith and Jth observations (I < J), either use the formula
D((I-1)*(M-I/2)+J-I), or use the helper function Z = SQUAREFORM(D),
which returns an M-by-M square symmetric matrix, with the (I,J) entry
equal to distance between observation I and observation J.

Example:
% Compute the ordinary Euclidean distance
X = randn(100, 5);                 % some random points
D = pdist(X, 'euclidean');         % euclidean distance

% Compute the Euclidean distance with each coordinate difference
% scaled by the standard deviation
Dstd = pdist(X,'seuclidean');

% Use a function handle to compute a distance that weights each
% coordinate contribution differently
Wgts = [.1 .3 .3 .2 .1];           % coordinate weights
weuc = @(XI,XJ,W)(sqrt(bsxfun(@minus,XI,XJ).^2 * W'));
Dwgt = pdist(X, @(Xi,Xj) weuc(Xi,Xj,Wgts));


‘euclidean’，’seuclidean’，’cityblock’，’minkowski’，’chebychev’，’mahalanobis’，’mahalanobis’，’cosine’，’correlation’，’spearman’，’hamming’，’jaccard’分别对应着默认的欧式距离L2标准欧式距离城市距离（曼哈顿距离）闵可夫斯基距离切比雪夫距离马氏距离夹角余弦皮尔逊相关距离斯皮尔曼等级相关距离（1-斯皮尔曼等级相关系数）汉明距离杰卡德相似距离（1-Jaccard similarity coefficient）。还可以使用自定义的距离函数：Function，function D2 = DISTFUN(XI, XJ)。在有需要的时候再进行说明：

#### 欧式距离L2

X = [0 0 ; 1 0 ; 0 2]
D = pdist(X,'euclidean')
D =
1.0000    2.0000    2.2361


#### 标准欧式距离

dab=k=1n(x1kx2kσk)2

X = [0 0 ; 1 0 ; 0 2]
D = pdist(X, 'seuclidean',[0.5,1])
D =

2.0000    2.0000    2.8284


#### 城市距离（曼哈顿距离）

dab=k=1n|x1kx2k|

X = [0 0 ; 1 0 ; 0 2]
D = pdist(X, 'cityblock')
D =
1     2     3


#### 闵可夫斯基距离

X = [0 0 ; 1 0 ; 0 2]
D = pdist(X,'minkowski',2)
D =

1.0000    2.0000    2.2361


#### 切比雪夫距离

X = [0 0 ; 1 0 ; 0 2]
D = pdist(X, 'chebychev')
D =

1     2     2


#### 马氏距离

X = [1 2; 1 3; 2 2; 3 1]
Y = pdist(X,'mahalanobis')
Y =
2.3452    2.0000    2.3452    1.2247    2.4495    1.2247


#### 夹角余弦

X = [1 0 ; 1 1.732 ; -1 0]
D = 1- pdist(X, 'cosine')  % Matlab中的pdist(X, 'cosine')得到的是1减夹角余弦的值
D =
0.5000   -1.0000   -0.5000


#### 皮尔逊相关距离

(1)、两个变量之间是线性关系，都是连续数据。
(2)、两个变量的总体是正态分布，或接近正态的单峰分布。
(3)、两个变量的观测值是成对的，每对观测值之间相互独立。

Matlab计算(1, 2 ,3 ,4 )与( 3 ,8 ,7 ,6 )之间的相关系数与相关距离

X = [1 2 3 4 ; 3 8 7 6]
C = corrcoef( X' )   %将返回相关系数矩阵
D = pdist( X , 'correlation')


#### 斯皮尔曼等级相关距离（1-斯皮尔曼等级相关系数）

coeff = corr(X , Y , 'type' , 'Spearman');


#### 汉明距离

Matlab中2个向量之间的汉明距离的定义为2个向量不同的分量所占的百分比。

 X = [0 0 ; 1 0 ; 0 2];
D = PDIST(X, 'hamming')
D = 0.5000    0.5000    1.0000


#### 杰卡德相似距离（1-Jaccard similarity coefficient）

p ：样本A与B都是1的维度的个数

q ：样本A是1，样本B是0的维度的个数

r ：样本A是0，样本B是1的维度的个数

s ：样本A与B都是0的维度的个数

Matlab的pdist函数定义的杰卡德距离跟这里的定义有一些差别，Matlab中将其定义为不同的维度的个数占“非零维度个数”的比例，意思就是比如下面的(1,1,0)、(1,-1,0)，不同的维度的个数有1个，只有第二个维度不同，两个集合中的非零元素个数为2个，1和-1嘛，所以，对应的杰卡德相似系数为0.5。

X = [1 1 0; 1 -1 0; -1 1 0]
D = pdist( X , 'jaccard')
D =
0.5000    0.5000    1.0000


2015-11-07 调试记录 张朋艺

posted @ 2015-11-07 18:44  ZhangPYi  阅读(234)  评论(0编辑  收藏  举报