??? In an assignment A(I) = B, the number of elements in B and I must be the same.

1 回表示 (過去 30 日間)
baby
baby 2012 年 5 月 11 日
hello,,
i need help becoz something's wrong with my code,,
please help me,,
that error show that -->>
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> w1(i)=w1(i)+dw1(i);
please help me,,
this is my homework and i must collect it tomorrow,,
my Code
p1=[0 1 0 2];
p2=[0 1 0 2];
t=[1 1 1 0];
%Weight
w1=0;
w2=0;
b=1;
teta=0;
w1new=1;
%Iteration
for j=1:3
for i=1:5
%Calculate n
n(i)=w1*p1(i)+w2*p2(i)+b;
%Calculate Output
if n(i)>teta
a(i)=1;
else
a(i)=0;
end
%calculate error value
error(i)= t(i) - a(i);
dw1(i)=error(i)*p1(i);
dw2(i)=error(i)*p2(i);
db{i}=error(i);
w1(i)=w1(i)+dw1(i);
w2(i)=w2(i)+dw2(i);
b(i)=b(i)+db{i};
end
end
disp(['P1 : ',num2str(p1)]);
i dont know what's wrong with my code,,,
please help me :(
  4 件のコメント
Daniel Shub
Daniel Shub 2012 年 5 月 11 日
Over writing MATLAB functions with variables (e.g., i, j, error, and db) is bad style and can lead to all sort of problems.
Greg Heath
Greg Heath 2012 年 7 月 15 日
Why are you using loops when your result can be obtained by matrix operations?

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

採用された回答

Image Analyst
Image Analyst 2012 年 5 月 11 日
I have no idea what this does, or if my "fix" below is what you want, but at least it runs. You didn't initialize p1 and p2 to have the required 5 elements, and you didn't use w1 and w2 as matrices everywhere.
p1=[0 1 0 2 0];
p2=[0 1 0 2 0];
t=[1 1 1 0 0];
%Weight
w1 = zeros(1,5);
w2= zeros(1,5);
b= ones(1, 5);
teta=0;
w1new=1;
%Iteration
for j=1:3
for i=1:5
%Calculate n
n(i)=w1(i)*p1(i)+w2(i)*p2(i)+b(i);
%Calculate Output
if n(i)>teta
a(i)=1;
else
a(i)=0;
end
%calculate error value
error(i)= t(i) - a(i);
dw1(i)=error(i)*p1(i);
dw2(i)=error(i)*p2(i);
db{i}=error(i);
w1(i)=w1(i)+dw1(i);
w2(i)=w2(i)+dw2(i);
b(i)=b(i)+db{i};
end
end
disp(['P1 : ',num2str(p1)]);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by