⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codebook.m

📁 a matlab program that deals with k-means algorithm to cluster the data points and build codebook.
💻 M
字号:
function codebook
load 'X.mat' 
X_test = X(:, 10001:end); % 40,000 points are taken as testing data
X = X(:,1:5000); % first 5000 points are training points

centroid = k_means(X); % finding the centroids of the data
X_test(3,:) = 0; %initializing the third row for class label
fid = fopen('codebook.txt', 'wt'); % alternative storage of class label or codebook

for m = 1:length(X_test),
    testing = [X_test(1:2,m) centroid]; % imbedding the test point with the centroids
    dist = squareform(pdist(testing')); % finding the distances from the test point to all the centroids
    dist = dist(1,2:6); % saving only first row leaving first column
    label = find(dist == min(dist)); % 'label' is the class that has the minimun distance between test point and centroids
    X_test(3,m) = label; % labelling the test point
    
    % creating a codebook, named codebook.txt
    switch (label)
        case 1,
            fprintf(fid, '000\n');
        case 2,
            fprintf(fid, '001\n');
        case 3,
            fprintf(fid, '010\n');
        case 4, 
            fprintf(fid, '011\n');
        case 5, 
            fprintf(fid, '100\n');
    end
end
save codebook_test X_test; % saving the test data with the corresponding label of class as codebook_test
fclose(fid);

% so there are two places where the codebook is saved. 
% first is the mat file: codebook_test
% this codebook_test, has three rows, first two rows correspond to test
% point and the final row is label of that point. 
% second place is file named 'codebook.txt'.
% in this file, the indexes of the all the testing data are saved. 
% 000 corresponds to class 1
% 001 corresponds to class 2
% 010 corresponds to class 3
% 011 corresponds to class 4 and 
% 100 corresponds to class 5. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -