got error 'Attempt to execute SCRIPT pca as a function'

1 回表示 (過去 30 日間)
MUHAMMAD  ADNAN
MUHAMMAD ADNAN 2015 年 5 月 18 日
編集済み: Rik 2020 年 4 月 29 日
w = 1./var(jointdata);
[wcoeff,score,latent,tsquared] = pca(jointdata,...
'VariableWeights');
Attempt to execute SCRIPT pca as a function:
C:\Users\Adnan Gujjar\Desktop\MSProject\pca.m
i want to apply PCA in it why i got error can any one help me??
  4 件のコメント
MUHAMMAD  ADNAN
MUHAMMAD ADNAN 2015 年 5 月 18 日
編集済み: Guillaume 2015 年 5 月 18 日
this is whole ccode that i run in to matlab i have 8 variable numerial data.
load jointdata
figure()
boxplot(jointdata,'orientation','horizontal');
C = corr(jointdata,jointdata);
w = 1./var(jointdata);
[wcoeff,score,latent,tsquared,explained] = pca(jointdata,...
'VariableWeights',w);
c3 = wcoeff(:,1:3);
coefforth = inv(diag(std(jointdata)))*wcoeff;
I = c3'*c3;
cscores = zscore(jointdata)*coefforth;
figure()
plot(score(:,1),score(:,2),'+');
xlabel('1st Principal Component');
ylabel('2nd Principal Component');
Jan
Jan 2015 年 5 月 18 日
@Muhammad: Please answer Stephens question: What do you get when to type this command in the command window:
which pca
Or even better:
which pca -all

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 5 月 18 日
Your file C:\Users\Adnan Gujjar\Desktop\MSProject\pca.m does not have its first non-comment line starting with
function SOMETHING = pca(SOMETHING)
It needs to do so if you want to call upon it to return a calculated result.
  7 件のコメント
MUHAMMAD  ADNAN
MUHAMMAD ADNAN 2015 年 5 月 18 日
Dear i change filename i still face same problem. please look it and correct me code.
Walter Roberson
Walter Roberson 2015 年 5 月 18 日
What filename are you using now? And exactly what error message are you getting now?

サインインしてコメントする。


Hassan Macanga
Hassan Macanga 2020 年 4 月 28 日
編集済み: Walter Roberson 2020 年 4 月 29 日
clc
clear
input_dir = 'user/mac/desktop/python/eigenfacematlab/eigenface24/train';
testdir= 'user/mac/desktop/python/eigenfacematlab/eigenface24/test';
image_dims = [200, 180];
classes=[0;1;2;3;4;5;6;7;8;9];
num_images = 170;
images = [];
imageclasses=[];
disp(num_images);
counter = 1;
c = cell(170,1);
c{1}={''};
file=cell(170,1);
file(1)={''};
paths = fullfile(testdir,{'0','1','2','3','4','5','6','7','8','9'});
pathsA = fullfile(testdir,{'0','1','2','3','4','5','6','7','8','9'});
for i = classes(1):classes(10)
filenames = dir(fullfile(input_dir,int2str(i),'\*.jpg'));
num_images = size(filenames,1);
for n = 1:num_images
%filenames = dir(fullfile(input_dir, '*.jpg'));
%disp(filenames)
filename = fullfile(input_dir,int2str(i), filenames(n).name);
disp(filename)
ij=cellstr(filename);
k=cellstr(filenames(n).name);
file{counter}=(k);
disp(filename)
img = imread(filename);
img = rgb2gray(img);
img = im2double(img);
c{counter}=(1i);
images(:, counter) = img(:);
imagesclasses(:,counter)=i;
counter=counter+1;
end
end
num_images = 170;
counter = 170;
mean_face = mean(images, 2);
shifted_images = images - repmat(mean_face, 1, num_images);
[u, s, v] = pca(images');
num_eigenfaces = 20;
u = u(:, 1:num_eigenfaces);
features = u' * shifted_images;
testlabel=[];
cc=1;
testimages=[];
featuresvector=[];
predicition=[];
%testing
for i = classes(1):classes(10)
filenames1 = dir(fullfile(testdir,int2str(i),'\*.jpg'));
for n = 1:3
filename1 = fullfile(testdir,int2str(i),filenames1(n).name);
img = imread(filename1);
img = rgb2gray(img);
img = im2double(img);
figure(1)
imshow(img)
% if n == 1
% images = zeros(prod(image_dims), num_images);
% end
input_image(:, cc) = img(:);
testlabel(:,cc)=i;
feature_vec = u' * (img(:) - mean_face);
similarity = arrayfun(@(n) 1 / (1 + norm(features(:,n) - feature_vec)), 1:num_images);
% find the image with the highest similarity
[match, matchidx] = max(similarity);
A = reshape(images(:,matchidx), image_dims);
Predicition(:,cc)=imagesclasses(matchidx);
% display the result
%figure
%A=cell2struct(file,'name',1)
if 3%n=0
figure
imshow([img reshape(images(:,matchidx), image_dims)]);
title(sprintf('matches %f, score %0.2f', 1-match));
end
cc=cc+1;
end
end
confusionMatrix=confusionmat(testlabel,predicition);
%show mean face
a = reshape(mean_face, image_dims);
figure
imshow(a)
% display the eigenfaces
figure;
for n = 1:num_eigenvalues
subplot(2, ceil(num_eigenvalues/4), n);
ejector = reshape(u(:,n), image_dims);
images(ejector);
colormap(gray);
end
  3 件のコメント
Rik
Rik 2020 年 4 月 28 日
編集済み: Rik 2020 年 4 月 29 日
This is not an answer, but a question. Please post it as your own question. Feel free to post a link to your question here.
For your reposted question, consider the following changes:
  • either attach your m-file or use proper layout to make your question more readable
  • post an exact description of what you are doing that produces this error (what is your current directory and how are you calling this script)
Walter Roberson
Walter Roberson 2020 年 4 月 29 日
You have a script (not a function) named eigenfaces.m which you are attempting to invoke as if it were a function. However it is not clear to us where you are doing that, as you do not use eigenfaces in your code. Perhaps the file you posted is itself named eigenfaces.m and you are somehow invoking it as if it were a function.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDimensionality Reduction and Feature Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by