Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
Daniel Shub
Daniel Shub on 11 May 2012
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 on 15 Jul 2012
Why are you using loops when your result can be obtained by matrix operations?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 11 May 2012
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)]);

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!