Principal component analysis pca

1 view (last 30 days)
HI ALL, i need to help me when i run this code of pca i find this error please help me its urgent Thanks.
this is my error:
Undefined variable data.
Error in new_pca (line 35)
Training ( size ( Training ,1) +1 ,:)= data( counter ,:) ;
clc
clear all
% The total number of samples in each class is denoted by NSamples
NSamples =3;
% The number of classes
NClasses =30;
counter =1;
% Read the data
S=[];% S will store all the images
figure(1);
for i=1:NClasses
str = strcat('D:\eigenfaces\s\', int2str(i));
str = strcat(str, '.jpg');
eval('I=imread(str);');
I = rgb2gray(I);
I = imresize(I, [50,50]);
[m n]=size(I);
subplot(ceil(sqrt(NClasses)),ceil(sqrt(NClasses)),i)
%imshow(I)
if j==NSamples
title('TrainingDatasets','fontsize',18)
% Each image is represented by one column
data( counter ,:)=I (:) ;
% Y is the class label matrix
Y( counter ,1)=NClasses;
counter = counter +1;
end
drawnow;
end
NTrainingSamples =2;
counter =1;
Training =[]; Testing =[]; TrLabels =[]; TestLabels =[];
for i=1: NClasses
for j=1: NTrainingSamples
Training ( size ( Training ,1) +1 ,:)= data( counter ,:) ;
TrLabels ( size ( TrLabels ,1) +1 ,1)= Y( counter ,1) ;
counter = counter +1;
end
for j= NTrainingSamples +1: NSamples
Testing ( size ( Testing ,1) +1 ,:)= data( counter ,:);
TestLabels ( size ( TestLabels ,1) +1 ,:)= Y( counter ,1);
counter = counter +1;
end
end
clear data Y I;
tic

Accepted Answer

sarine_nassima
sarine_nassima on 17 Oct 2019
i have error in tow lines if any one correcte me probleme please .thank you
% Each image is represented by one column
data( counter ,:)=I (:) ;
% Y is the class label matrix
Y( counter ,1)=NClasses;

More Answers (1)

Walter Roberson
Walter Roberson on 22 Oct 2019
Edited: Walter Roberson on 22 Oct 2019
if j==NSamples
You did not assign to j by that point in the code, so it has its default value of sqrt(-1) which does not equal NSamples, so you never assign to data

Categories

Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!