机器学习必备的计算机编程技巧(matlab、python)和总结——第一蛋

来源:互联网 发布:央视网络电视台官网 编辑:程序博客网 时间:2024/06/06 04:51

pdist2

Pairwise distance between two sets of observations

Syntax

D = pdist2(X,Y)
D = pdist2(X,Y,distance)
D = pdist2(X,Y,'minkowski',P)
D = pdist2(X,Y,'mahalanobis',C)

D = pdist2(X,Y) returns a matrix D containing the Euclidean distances between each pair of observations in the mx-by-n data matrix X and my-by-n data matrix Y. Rows of X and Y correspond to observations, columns correspond to variables. D is an mx-by-my matrix, with the (i,j) entry equal to distance between observation i in X and observation j in Y. The (i,j) entry will be NaN if observation i in X or observation j in Y contain NaNs.

D = pdist2(X,Y,distance) computes D using distance. Choices are:


'euclidean'

Euclidean distance (default).

min

Smallest elements in array

collapse all in page

Syntax

  • M = min(A)
  • M = min(A,[],dim)

  • [M,I] = min(___)

  • C = min(A,B)

  • ___ = min(___,nanflag)

Description

example

M = min(A) returns the smallest elements of A.

  • If A is a vector, then min(A) returns the smallest element of A.

  • If A is a matrix, then min(A) is a row vector containing the minimum value of each column.

  • If A is a multidimensional array, then min(A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same. If A is an empty array with first dimension 0, then min(A) returns an empty array with the same size as A.

example

M = min(A,[],dim) returns the smallest elements along dimension dim. For example, if A is a matrix, then min(A,[],2) is a column vector containing the minimum value of each row.

example

[M,I] = min(___) finds the indices of the minimum values of A and returns them in output vector I, using any of the input arguments in the previous syntaxes. If the minimum value occurs more than once, thenmin returns the index corresponding to the first occurrence.


[C,idx(i)]=min(distanse); 
distance is a vector
output: C stands for the smallest item in the Vector
        meanwhile, idx(i) the location of the item.


isempty

Determine whether array is empty

Syntax

TF = isempty(A)

Description

TF = isempty(A) returns logical 1 (true) if A is an empty array and logical 0 (false) otherwise. An empty array has at least one dimension of size zero, for example, 0-by-0 or 0-by-5.

Examples

B = rand(2,2,2);B(:,:,:) = [];isempty(B)
ans = 1

exist

Check existence of variable, function, folder, or class

collapse all in page

Syntax

exist name
exist name kind

A = exist('name','kind')




builtin

Checks only for built-in functions.

class

Checks only for classes.

dir

Checks only for folders.

file

Checks only for files or folders.

var

Checks only for variables.


if ~exist('plot_progress', 'var') || isempty(plot_progress)


||  means       logistic     OR!


centroids(:,1)

centroid =

2.4283   3.1579
5.8135   2.6336
7.1193   3.6166


centroids(:,1)
ans =
    2.4283
    5.8135
    7.1194
>> centroids(:,2)
ans =
    3.1579
    2.6337
    3.6167

How to select random items in X
size(X) = 100*2

randnum=randperm(length(X));
initial_centroids =X(randnum(1:5), :);

imread

Read image from graphics file

collapse all in page

Syntax

  • A = imread(filename)
    example
  • A = imread(filename,fmt)
  • A = imread(___,idx)
  • A = imread(___,Name,Value)
    example
  • [A,map] = imread(___)
    example
  • [A,map,transparency] = imread(___)
    example

Description

example

A = imread(filename) reads the image from the file specified by filename, inferring the format of the file from its contents. If filename is a multi-image file, then imread reads the first image in the file.

A = imread(filename,fmt) additionally specifies the format of the file with the standard file extension indicated by fmt. If imread cannot find a file with the name specified by filename, it looks for a file named filename.fmt.

A = imread(___,idx) reads the specified image or images from a multi-image file. This syntax applies only to GIF, CUR, ICO, and HDF4 files. You must specify a filename input, and you can optionally specify fmt.

example

A = imread(___,Name,Value) specifies format-specific options using one or more name-value pair arguments, in addition to any of the input arguments in the previous syntaxes.

example

[A,map] = imread(___) reads the indexed image in filename into A and reads its associated colormap into map. Colormap values in the image file are automatically rescaled into the range [0,1].


ind2rgb

Convert indexed image to RGB image

Syntax

RGB = ind2rgb(X,map)


[A,map] = imread(___) reads the indexed image in filename into A and reads its associated colormap into map. Colormap values in the image file are automatically rescaled into the range [0,1].


什么是索引图像?

图中圆圈内的就是索引图像的索引表,下面的就是其对应RGB颜色表。

图中小圆圈处的索引号是5,对应RGB颜色的第5行,所以该处RGB颜色实际是0.2902  0.0627 0.0627 

索引图像的作用就是体积小,方便传输,只需要把索引表传输过去,接收方用对应的RGB颜色表还原就行。




RGB图像模型。

三维坐标:


原点到白色顶点的中轴线是灰度线,r、g、b三分量相等,强度可以由三分量的向量表示。

用RGB来理解色彩、深浅、明暗变化:

色彩变化: 三个坐标轴RGB最大分量顶点与黄紫青YMC色顶点的连线

深浅变化:RGB顶点和CMY顶点到原点和白色顶点的中轴线的距离

明暗变化:中轴线的点的位置,到原点,就偏暗,到白色顶点就偏亮


HSV图像模型

倒锥形模型:


这个模型就是按色彩、深浅、明暗来描述的。

H是色彩

S是深浅, S = 0时,只有灰度

V是明暗,表示色彩的明亮程度,但与光强无直接联系,(意思是有一点点联系吧)。




3. RGB与HSV的联系

从上面的直观的理解,把RGB三维坐标的中轴线立起来,并扁化,就能形成HSV的锥形模型了。

但V与强度无直接关系,因为它只选取了RGB的一个最大分量。而RGB则能反映光照强度(或灰度)的变化。

v = max(r, g, b)

由RGB到HSV的转换:


HSV对用户来说是一种直观的颜色模型。我们可以从一种纯色彩开始

即指定色彩角H,并让V=S=1,然后我们可以通过向其中加入黑色和白色

来得到我们需要的颜色。增加黑色可以减小V而S不变

同样增加白色可以减小S而V不变。例如,要得到深蓝色,V=0.4 S=1 H=240度。

要得到淡蓝色,V=1 S=0.4 H=240度。" --百度

其他格式,VSH,等点我

reshape把指定的矩阵改变形状,但是元素个数不变

例如,行向量:
a = [1 2 3 4 5 6]

执行下面语句把它变成3行2列:
b = reshape(a,3,2)

执行结果:
b =
1 4
2 5
3 6

若a=[1 2 3
4 5 6
7 8 9]

使用reshpe后想得到b=[1 2 3 4 5 6 7 8 9]

只需要将a转置一下就可以了:
b=reshape(a',1,9)

A = 1:10;B = reshape(A,[5,2])

B =     1     6     2     7     3     8     4     9     5    10

Reshape Multidimensional Array into Matrix

Open This Example

Reshape a 3-by-2-by-3 array of zeros into a 9-by-2 matrix.

A = zeros(3,2,3);B = reshape(A,9,2)
B =     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

subplot

Create axes in tiled positions

collapse all in page

Syntax

  • subplot(m,n,p)
    example
  • subplot(m,n,p,'replace')
    example
  • Description

    example

    subplot(m,n,p) divides the current figure into an m-by-n grid and creates an axes for a subplot in the position specified by p. MATLAB® numbers its subplots by row, such that the first subplot is the first column of the first row, the second subplot is the second column of the first row, and so on. If the axes already exists, then the command subplot(m,n,p) makes the subplot in position p the current axes.

    example

    subplot(m,n,p,'replace') deletes any existing axes in position p and creates a new axes.



imagesc

Display image with scaled colors

collapse all in page

Syntax

  • imagesc(C)
    example
  • imagesc(x,y,C)



阅读全文
0 0
原创粉丝点击