天天看點

機器學習必備的計算機程式設計技巧(matlab、python)和總結——第一蛋pdist2minisemptyexistcentroids(:,1)imreadind2rgbsubplotimagesc

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 

NaN

s.

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  , 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, then

min

 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顔色表還原就行。

機器學習必備的計算機程式設計技巧(matlab、python)和總結——第一蛋pdist2minisemptyexistcentroids(:,1)imreadind2rgbsubplotimagesc

RGB圖像模型。

三維坐标:

原點到白色頂點的中軸線是灰階線,r、g、b三分量相等,強度可以由三分量的向量表示。

用RGB來了解色彩、深淺、明暗變化:

色彩變化: 三個坐标軸RGB最大分量頂點與黃紫青YMC色頂點的連線

深淺變化:RGB頂點和CMY頂點到原點和白色頂點的中軸線的距離

明暗變化:中軸線的點的位置,到原點,就偏暗,到白色頂點就偏亮

HSV圖像模型

倒錐形模型:

機器學習必備的計算機程式設計技巧(matlab、python)和總結——第一蛋pdist2minisemptyexistcentroids(:,1)imreadind2rgbsubplotimagesc

這個模型就是按色彩、深淺、明暗來描述的。

H是色彩

S是深淺, S = 0時,隻有灰階

V是明暗,表示色彩的明亮程度,但與光強無直接聯系,(意思是有一點點聯系吧)。

機器學習必備的計算機程式設計技巧(matlab、python)和總結——第一蛋pdist2minisemptyexistcentroids(:,1)imreadind2rgbsubplotimagesc

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.
    機器學習必備的計算機程式設計技巧(matlab、python)和總結——第一蛋pdist2minisemptyexistcentroids(:,1)imreadind2rgbsubplotimagesc
機器學習必備的計算機程式設計技巧(matlab、python)和總結——第一蛋pdist2minisemptyexistcentroids(:,1)imreadind2rgbsubplotimagesc

imagesc

Display image with scaled colors

collapse all in page

Syntax

  • imagesc(C)

    example
  • imagesc(x,y,C)

繼續閱讀