Here is the code:
function [ mRgbLut ] = CreateRgbLUT( mCodeBook )
numLevels = size(mCodeBook, 1);
vBaseValues = [0:255];
mCombValues = allcomb(vBaseValues, vBaseValues, vBaseValues);
mCombValues = [mCombValues(:, 3), mCombValues(:, 2), mCombValues(:, 1)];
mCodeBookReshaped = reshape(mCodeBook.', [1, 3, numLevels]);
mRgbDistance = bsxfun(@minus, mCombValues, mCodeBookReshaped) .^ 2;
mRgbDistance = sum(mRgbDistance, 2);
[minRgbDist, codeBookIdx] = min(mRgbDistance, [], 3);
mRgbLut = reshape(codeBookIdx, [256, 256, 256]);
end
I hope this might assist someone (It something like the step in K Means).