在Coding时经常需要复用一些之前写过的代码,开始有意识的将一些重复时候的代码独立成函数,方便后面调用。 在对图像提取特征后经常需要对其进行归一化,归一化方法很多,如对某一列向量 x⃗ ,找出该列向量中的 max(x⃗ ) , min(x⃗ ) ,对于列向量中某一元素,便可以利用 xi−min(x⃗ )xi−max(x⃗ ) 对其进行归一化,除了利用这种方法进行归一化外,我们还可以对向量 x⃗ 求和,即 ∑x⃗ ,列向量 x⃗ 元素进行归一化 xi∑x⃗ ,下面函数实现对矩阵采用第二种方法进行归一化。
1 | function normalizedX= nomalize( X ) |
2 | % This function impliments normalizing columns of the matrix X |
3 | % Input : X is a m*n matrix |
4 | % Output: normalizedX is a normalied matrix each column |
5 | % is normalized to [0,1], and the sum of each cilumn is 1. |
6 | % |
7 | % Written by Willard |
8 | % Data: 2013/10/06 |
9 | % |
10 | % Example: X = 1 2 |
11 | % 3 4 |
12 | % After normalizing: normalizedX= 0.2500 0.3333 |
13 | % 0.7500 0.6667 |
14 | |
15 | normalizedX=bsxfun(@times, X, 1./(sum(X, 1))); |