matrix dimensions must agree
1 次查看(过去 30 天)
显示 更早的评论
Please help me i have a problem with my program neural network about matrix. What program should I add that the error disappear??
clc
clear all
%==========================================================================
% PENGOLAHAN CITRA
%==========================================================================
%histogram
jlhkelas = 3;
jlh = 20;
dir1 = 'training\';
%dir2 = 'testing\';
for kelas = 1:jlhkelas
for byk = 1:jlh
dir = strcat(dir1,int2str(kelas),'-',int2str(byk),'.jpg');
img=imread(dir);
imgred = img(:,:,1);
imggreen = img(:,:,2);
imgblue = img(:,:,3);
[row col dim]= size(img);
imgb = ones(row+2,col+2,3);
imgb = im2uint8(imgb);[row col dim]= size(img);
imgb = ones(row+2,col+2,3);
imgb = im2uint8(imgb);
for i = 1:row
for j = 1:col
imgb(i+1,j+1,1) = imgred(i,j);
imgb(i+1,j+1,2) = imggreen(i,j);
imgb(i+1,j+1,3) = imgblue(i,j);
end
end
%mengatur kecerahan gambar
OUTPUT = strcat(dir1,int2str(kelas),'-',int2str(byk),'.jpg');
a=rgb2gray(img);
a = imadjust(a,stretchlim(a),[]);
[row col] = size(a);
x = ones(row+2,col+2);
x = im2uint8(x);
for i =1:row
for j=1:col
x(i+1,j+1)= a(i,j);
end
end
%untuk mengubah gambar ke dalam bentuk nilai biner
gray=rgb2gray(img);
thresh=graythresh(gray);
imbw=im2bw(gray,thresh);
end
end
%==========================================================================
% PENENTUAN FITUR DARI DAUN KAKAO SEBAGAI INPUT KE JST
%==========================================================================
%Grayscale
baris = 1
for i = 1:jlhkelas
for j = 1:jlh
dir = strcat(dir1,int2str(i),'-',int2str(j),'.jpg');
I = imread(dir);
I = rgb2gray(I);
Ih = imhist(I);
Ih = Ih(20:220);
Ih(Ih == 0) = [];
%Mean
rata2 = mean(Ih);
%Standar Deviasi
sdeviasi = std(Ih);
%Kurtosis
K = kurtosis(Ih);
%Skewness
Sk = skewness(Ih);
[p,SI] = graycomatrix(I,'NumLevels',max(max(I))+1,'G',[]);
p(p==0) = [];
p = p/sum(p);
%Entropy
E = -sum((p).*log2(p));
MGray(baris,:) = [rata2 sdeviasi K Sk E];
%RGB Normalisasi
Image_rgb = imread(dir);
Image_rgb = double(Image_rgb);
Image_red = Image_rgb(:,:,1);
Image_green = Image_rgb(:,:,2);
Image_blue = Image_rgb(:,:,3);
[row,col] = size(Image_rgb(:,:,1));
for y = 1:row %-->numberof rows in image
for x = 1:col %-->number of columns in the image
Red = Image_red(y,x);
Green = Image_green(y,x);
Blue = Image_blue(y,x);
NormalizedRed = Red /sqrt(Red^2 + Green^2 + Blue^2);
NormalizedGreen = Green/sqrt(Red^2 + Green^2 + Blue^2);
NormalizedBlue = Blue /sqrt(Red^2 + Green^2 + Blue^2);
Image_red(y,x) = NormalizedRed;
Image_green(y,x) = NormalizedGreen;
Image_blue(y,x) = NormalizedBlue;
end
end
Image_rgb(:,:,1) = (Image_red);
Image_rgb(:,:,2) = (Image_green);
Image_rgb = Image_rgb(:,:,1).*Image_rgb(:,:,2);
Img = im2uint8(Image_rgb);
Imgb = im2double(Img);
Imgb = imhist (Imgb);
Imgb(Imgb==0) = [];
%MeanRGB
rata2RGB = mean(Imgb);
%Standar DeviasiRGB
sdeviasiRGB = std(Imgb);
%KurtosisRGB
KRGB = kurtosis(Imgb);
%SkewnessRGB
SkRGB = skewness(Imgb);
[p1,SI] = graycomatrix(Img,'NumLevels',max(max(Img))+1,'G',[]);
p1(p1==0) = [];
p1 = p1/sum(p1);
%EntropyRGB
ERGB = -sum((p1).*log2(p1));
MRGB(baris,:) = [rata2RGB sdeviasiRGB KRGB SkRGB ERGB];
%Pengaturan Kecerahan Image
a = I;
a = imadjust(a,stretchlim(a),[]);
[row col] = size(a);
x = ones(row+2,col+2);
x = im2uint8(x);
for in =1:row
for jn=1:col
x(in+1,jn+1)= a(in,jn);
end
end
%Level Saturasi4
Image_Sat = imread(dir);
hsv = rgb2hsv(Image_Sat);
s = hsv(:,:,2);
s = im2uint8(s);
shist = imhist(s);
shist = shist(20:220);
shist(shist == 0) = [];
%===========MeanSat
rata2Sat = mean(shist);
%===========Standar DeviasiSat
sdeviasiSat = std(shist);
%===========KurtosisSat
KSat = kurtosis(shist);
%===========SkewnessSat
SkSat = skewness(shist);
%===========EntropySat
[p3,SI] = graycomatrix(s,'NumLevels',max(max(s))+1,'G',[]);
p3(p3==0) = [];
p2 = p3/sum(p3);
ESat = -sum((p3).*log2(p3));
MSat(baris,:) = [rata2Sat sdeviasiSat KSat SkSat ESat];
baris = baris +1;
end
end
Hasil = [(1:baris-1)' MGray MRGB MSat];
P = ([MGray MRGB MSat])';
P = (P)/1e+002
T =[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
% Preprocessing
[pn,meanp,stdp,tn,meant,stdt] = prestd(P,T);
% Membangun jaringan dengan propagasi umpan balik berbasis jaringan syaraf tiruan
net = newff(minmax(pn),[10 5 1],{'tansig' 'logsig' 'purelin'},'traingdm')
% melihat bobot awal jaringan
BobotAwal_Input = net.IW{1,1}
BobotAwal_Bias_Input = net.b{1,1}
BobotAwal_Lapisan1 = net.LW {2,1}
BobotAwal_Bias_Lapisan = net.b {2,1}
BobotAkhir_Lapisan2 = net.LW {3,2}
BobotAkhir_Bias_Lapisan2 = net.b{3,1}
% Set parameter pelatihan
net.trainParam.epochs = 1000;
net.trainParam.goal = 1e-04;
net.trainParam.lr = 0.6;
net.trainParam.showCommandLine = true;
net.trainParam.show = 100;
net.trainParam.mc = 0.7;
% Pelatihan
net = train(net,pn,tn);pause
% Melihat bobot akhir jaringan
BobotAkhir_Input = net.IW{1,1}
BobotAkhir_Bias_Input = net.b{1,1}
BobotAkhir_Lapisan1 = net.LW {2,1}
BobotAkhir_Bias_Lapisan = net.b {2,1}
BobotAkhir_Lapisan2 = net.LW {3,2}
BobotAkhir_Bias_Lapisan2 = net.b{3,1}
% Simulasi terhadap data pelatihan
an = sim(net,pn);
a = poststd(an,meant,stdt);
H = [(1:size(P,2))' T' a' (T'-a')];
sprintf('%2d %9.2f %7.2f %5.2f\n',H')
% Menggambar Grafik
[m1,a1,r1] = postreg(a,T)
pause;
plot ([1:size(P,2)]' ,T,'bo',[1:size(P,2)]',a','r*');
title('Hasil pengujian dengan data pelatihan Target (o) dan Output (*)');
xlabel('Data ke-');
ylabel('Target atau Output');
grid;
pause
%Input Data Baru
%Cek =load( 'TData.mat')
%Cek =load ('TData.mat','Q')
%Test =Q(:,1:30)';
%Q = 'TData.mat{(:,1:30)}';
Q = xlsread('data.xlsx');
TQ =[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1];
%Normalisasi input baru
Qn = trastd(Q,meanp,stdp);
bn = sim(net,Qn);
b = poststd(bn,meant,stdt);
L = [(1:size (Q,2))' T' b' (TQ' -b')];
sprintf('%2d %11.2f %9.2f %7.2f\n', L')
%Evaluasi output jaringan (data testing dengan target)
[m2,b1,r2] = postreg(b,TQ)
pause
k = [1:size(Q,2)]';
plot(k,TQ,'bo',k,b','r*');
title('Perbandingan antara Target (o) dan Output Jaringan (*)');
xlabel('input kedua');
ylabel('Target atau Output');
grid;
text(k+0.2*ones(lenght(k),1),TQ,int2str(k));
error:
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> trastd at 75
pn = (p-meanp*oneQ)./(stdp*oneQ);
Error in ==> fix at 244
Qn = trastd(Q,meanp,stdp);
4 个评论
回答(1 个)
Image Analyst
2013-11-27
First look at this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/. Then, before the pn= line, put these commands.
whos p
whos meanp
whos oneQ
whos stdp
whos oneQ
Tell us what it says in the command window.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics and Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!