Clear Filters
Clear Filters

Index exceeds matrix dimensions using pca in matlab 2013a

2 views (last 30 days)
here is the sample code:
temp_files = dir([temp_dir '\*.jpg']); im_waves = zeros(size(temp_files,1),2551);
for j1 = 1:size(temp_files,1)
filename = fullfile(temp_dir,temp_files(j1).name);
temp_im = imresize(double(imread(filename)),[50 50]);
% temp_im = imresize((imread(filename)),[50 50]);
[im_waves(j1,:),~] = feature_extraction_phase2(temp_im);
end
mean_waves = mean(im_waves);
centered_waves = im_waves - repmat(mean_waves,[size(temp_files,1) 1]);
[evectors,~, evalues] = princomp(centered_waves);
num_eigenfaces = 50;
evectors = evectors(:, 1:num_eigenfaces); // |the error is index exceeds matrix dimensions|
reduced_output_waves = evectors' * centered_waves';
reduced_input_waves = evectors' * (waves_ip - mean_waves)';
temp_score = arrayfun(@(n) 1 / (1 + norm(reduced_output_waves(:,n) - reduced_input_waves)), 1:size(temp_files,1));
score = [score,temp_score];
i checked with debugger too,
the centered
  2 Comments
Mahdi
Mahdi on 29 May 2014
Can you show the error message as well please(so we know which variable the error is in)?
Datti Nagadhara Harini
Datti Nagadhara Harini on 15 Jul 2014
Index exceeds matrix dimensions.
Error in wavelet_compare (line 28) evectors = evectors(:, 1:num_eigenfaces);

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 16 Jul 2014
Your code assumes that there are 50 columns in evectors. Why? With the debugger, prior to evaluating
evectors = evectors(:, 1:num_eigenfaces);
in the Command Window type
size(evectors)
and observe the results. It is most likely that the number of columns is less than num_eigenfaces. What you can do instead is
num_eigenfaces = min(size(evectors,2),50);
to use the minimum of the number of columns in evectors and 50. This way you won't encounter the index exceeds matrix dimension error.

Community Treasure Hunt

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

Start Hunting!