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

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by