Is there a way to remove double from the variable?

In the workspace, my variable is: lableimage < 359x476x3 double > Whenever I reshape it into a single column, the result is not 170884x1 but 512652x1. Also, because its double, the LDA is not working. Any suggestions?

8 Commenti

The variable size is 359x476x3, which contains 512652 elements, so it's reasonable to be 512652x1 when you reshape it to a single column.
What is LDA? Is it Linear Discriminant Analysis? What function do you use to perform LDA?
Yup. Using multi-class LDA. But the problem is when I insert in a double, it gives me errors...
Jing
Jing il 24 Gen 2013
Modificato: Jing il 24 Gen 2013
Can you put your code that generate the error as well as the error message?
Suggestion for posting questions here: please state your question as clear as possible, so that others can understand or even reproduce your problem, which will help to solve it quickly.
classdef LDA < handle %inherit from handle otherwise we must store value (object) each time when we change some propertie value
%function would need to return obj(this)
properties
Samples, %array of feature vectors
Classes, %class labels for samples
EigenVectors,
EigenValues,
BetweenScatter, %SB
WithinScatter, %SW
NumberOfClasses
end
properties(Access = 'private')
ClassSubsetIndexes %start and end indicies where one class begins and ends in variable "Classes"
TotalMean,
MeanPerClass
end
methods
function this = LDA(samples, classes)
this.Samples = samples;
this.Classes = classes;
this.ClassSubsetIndexes = LDA.GetClassSubsetIndexes(classes);
this.NumberOfClasses = size(this.ClassSubsetIndexes, 1);
[this.TotalMean this.MeanPerClass] = this.CalculateMean(samples);
end
function Compute(this)
SB = this.CalculateBetweenScatter();
SW = this.CalculateWithinScatter();
[eigVectors, eigValues] = eig(SB, SW);
eigValues = diag(eigValues);
%--------- sort eig values and vectors ---------%
sortedValues = sort(eigValues,'descend');
[c, ind] = sort(eigValues,'descend'); %store indicies
sortedVectors = eigVectors(:,ind); % reorder columns
this.EigenVectors = sortedVectors;
this.EigenValues = sortedValues;
this.BetweenScatter = SB;
this.WithinScatter = SW;
end
function projectedSamples = Transform(this, samples,
numOfDiscriminants)
vectors = this.EigenVectors(:, 1:numOfDiscriminants);
class(samples)
class(vectors)
size(samples)
size(vectors)
%transformed sample is scalar (projection on a hyperplane)
projectedSamples = samples * vectors;
end
function measure = CalculateFLDMeasure(this, numOfDiscriminants)
SB = this.BetweenScatter;
SW = this.WithinScatter;
vectors = this.EigenVectors(:, 1:numOfDiscriminants);
measure = det(vectors' * SB * vectors) / det(vectors' * SW * vectors);
end
end
methods(Access = 'private')
function [totalMean meanPerClass] = CalculateMean(this, samples)
for classIdx=1 : 1 : length(this.ClassSubsetIndexes)
startIdx = this.ClassSubsetIndexes(classIdx, 1);
endIdx = this.ClassSubsetIndexes(classIdx, 2);
meanPerClass(classIdx, :) = mean( samples
(startIdx:endIdx, :), 1);
end
totalMean = mean(meanPerClass, 1); %global average value
end
function SW = CalculateWithinScatter(this)
featureLength = size(this.Samples, 2);
SW = zeros(featureLength, featureLength);
for classIdx=1 : 1 : length(this.ClassSubsetIndexes)
startIdx = this.ClassSubsetIndexes(classIdx, 1);
endIdx = this.ClassSubsetIndexes(classIdx, 2);
classSamples = this.Samples(startIdx:endIdx, :);
classMean = this.MeanPerClass(classIdx, :);
Sw_Class = LDA.CalculateScatterMatrix(classSamples, classMean);
SW = SW + Sw_Class;
end
end
function SB = CalculateBetweenScatter(this)
featureLength = size(this.Samples, 2);
SB = zeros(featureLength, featureLength);
for classIdx=1 : 1 : length(this.ClassSubsetIndexes)
startIdx = this.ClassSubsetIndexes(classIdx, 1);
endIdx = this.ClassSubsetIndexes(classIdx, 2);
numberOfSamplesInClass = endIdx - startIdx + 1;
classMean = this.MeanPerClass(classIdx, :);
%because my vector is row-vector
Sb_class = (classMean - this.TotalMean)' * (classMean - this.TotalMean);
Sb_class = numberOfSamplesInClass * Sb_class;
SB = SB + Sb_class;
end
end
end
methods(Static, Access = 'private')
function subset = GetClassSubsetIndexes(classes)
subset=[];
oldClassLabel = NaN;
for i=1 : 1 : length(classes)
if oldClassLabel ~= classes(i)
oldClassLabel = classes(i);
subset = cat(1, subset, i);
end
end
%now put end indicies
for i=2 : 1 : size(subset,1)
endIndex = subset(i, 1);
subset(i-1, 2) = endIndex-1;
end
subset(size(subset,1), 2) = length(classes);
end
function Sw_class = CalculateScatterMatrix(classSamples, classMean)
featureLength = size(classSamples, 2);
Sw_class = zeros(featureLength, featureLength);
for sampleIdx=1 : 1 : size(classSamples, 1)
covariance = (classSamples(sampleIdx, :) - classMean);
covariance = covariance' * covariance; %because my vector is row-vector
Sw_class = Sw_class + covariance;
end
end
end
end
Its not the problem with the code but with the data set, its all in double and uint8 so I cant get a result when put in...
Well, from such a long page of code, which line gives the error?
I thought you were using CLASSIFY to do LDA...
Line error : projectedSamples = samples * vectors;
---------------------------------------------------
Error:
Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.
Error in LDA/Transform (line 60) projectedSamples = samples * vectors;
---------------------------------------------------------------------
Input: data = importdata('LDA data.mat')
features=data(:,1:end-1); %split data without labels
lable=data(:,end); %get the labels
trainSamples = features;%training samples
trainClasses = lable;%training labels
I2 = reshape(I,[],1);
testSamples = I2;%test samples
lableimage = reshape(handles.lableimage,[],1);
testClasses = lableimage;%test labels
mLDA = LDA(trainSamples, trainClasses);
mLDA.Compute();
transformedTrainSamples = mLDA.Transform(trainSamples, 1);
transformedTestSamples = mLDA.Transform(testSamples, 1);
calculatedClases = knnclassify(transformedTestSamples, transformedTrainSamples, trainClasses);
simmilarity = [];
for i = 1 : 1 : length(testClasses)
similarity(i) = ( testClasses{i} == calculatedClases{i} );
end
accuracy = sum(similarity) / length(testClasses);
fprintf('Testing: Accuracy is: %f %%\n', accuracy*100);
guidata(hObject, handles);
The mtimes problem is being discussed in another earlier thread.

Accedi per commentare.

 Risposta accettata

To convert an array such as labelImage from being double to being uint8, use
newArray = uint8(labelImage);

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by