批處理感知器算法的代碼matlab
w1=[1,0.1,1.1;1,6.8,7.1;1,-3.5,-4.1;1,2.0,2.7;1,4.1,2.8;1,3.1,5.0;1,-0.8,-1.3;
1,0.9,1.2;1,5.0,6.4;1,3.9,4.0];
w2=[1,7.1,4.2;1,-1.4,-4.3;1,4.5,0.0;1,6.3,1.6;1,4.2,1.9;1,1.4,-3.2;1,2.4,-4.0;
1,2.5,-6.1;1,8.4,3.7;1,4.1,-2.2];
w3=[1,-3.0,-2.9;1,0.5,8.7;1,2.9,2.1;1,-0.1,5.2;1,-4.0,2.2;1,-1.3,3.7;1,-3.4,6.2;
1,-4.1,3.4;1,-5.1,1.6;1,1.9,5.1];
figure;
plot(w3(:,2),w3(:,3),'ro');
hold on;
plot(w2(:,2),w2(:,3),'b+');
W=[w2;-w3];%增廣樣本規范化
a=[0,0,0];
k=0;%記錄步數
n=1;
y=zeros(size(W,2),1);%記錄錯分的樣本
while any(y<=0)
k=k+1;
y=a*transpose(W);%記錄錯分的樣本
a=a+sum(W(find(y<=0),:));%更新a
if k >= 250
break
end
end
if k<250
disp(['a為:',num2str(a)])
disp(['k為:',num2str(k)])
else
disp(['在250步以內沒有收斂,終止'])
end
%判決面:x2=-a2*x1/a3-a1/a3
xmin=min(min(w1(:,2)),min(w2(:,2)));
xmax=max(max(w1(:,2)),max(w2(:,2)));
x=xmin-1:xmax+1;%(xmax-xmin):
y=-a(2)*x/a(3)-a(1)/a(3);
plot(x,y)
標簽:
批處理
算法matlab
上傳時間:
2016-11-07
上傳用戶:a1241314660
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
#define MaxSize 30
typedef struct athletestruct /*運動員*/
{
char name[20];
int score; /*分數*/
int range; /**/
int item; /*項目*/
}ATH;
typedef struct schoolstruct /*學校*/
{
int count; /*編號*/
int serial; /**/
int menscore; /*男選手分數*/
int womenscore; /*女選手分數*/
int totalscore; /*總分*/
ATH athlete[MaxSize]; /**/
struct schoolstruct *next;
}SCH;
int nsc,msp,wsp;
int ntsp;
int i,j;
int overgame;
int serial,range;
int n;
SCH *head,*pfirst,*psecond;
int *phead=NULL,*pafirst=NULL,*pasecond=NULL;
void create();
void input ()
{
char answer;
head = (SCH *)malloc(sizeof(SCH)); /**/
head->next = NULL;
pfirst = head;
answer = 'y';
while ( answer == 'y' )
{
Is_Game_DoMain:
printf("\nGET Top 5 when odd\nGET Top 3 when even");
printf("\n輸入運動項目序號 (x<=%d):",ntsp);
scanf("%d",pafirst);
overgame = *pafirst;
if ( pafirst != phead )
{
for ( pasecond = phead ; pasecond < pafirst ; pasecond ++ )
{
if ( overgame == *pasecond )
{
printf("\n這個項目已經存在請選擇其他的數字\n");
goto Is_Game_DoMain;
}
}
}
pafirst = pafirst + 1;
if ( overgame > ntsp )
{
printf("\n項目不存在");
printf("\n請重新輸入");
goto Is_Game_DoMain;
}
switch ( overgame%2 )
{
case 0: n = 3;break;
case 1: n = 5;break;
}
for ( i = 1 ; i <= n ; i++ )
{
Is_Serial_DoMain:
printf("\n輸入序號 of the NO.%d (0<x<=%d): ",i,nsc);
scanf("%d",&serial);
if ( serial > nsc )
{
printf("\n超過學校數目,請重新輸入");
goto Is_Serial_DoMain;
}
if ( head->next == NULL )
{
create();
}
psecond = head->next ;
while ( psecond != NULL )
{
if ( psecond->serial == serial )
{
pfirst = psecond;
pfirst->count = pfirst->count + 1;
goto Store_Data;
}
else
{
psecond = psecond->next;
}
}
create();
Store_Data:
pfirst->athlete[pfirst->count].item = overgame;
pfirst->athlete[pfirst->count].range = i;
pfirst->serial = serial;
printf("Input name:) : ");
scanf("%s",pfirst->athlete[pfirst->count].name);
}
printf("\n繼續輸入運動項目(y&n)?");
answer = getchar();
printf("\n");
}
}
void calculate() /**/
{
pfirst = head->next;
while ( pfirst->next != NULL )
{
for (i=1;i<=pfirst->count;i++)
{
if ( pfirst->athlete[i].item % 2 == 0 )
{
switch (pfirst->athlete[i].range)
{
case 1:pfirst->athlete[i].score = 5;break;
case 2:pfirst->athlete[i].score = 3;break;
case 3:pfirst->athlete[i].score = 2;break;
}
}
else
{
switch (pfirst->athlete[i].range)
{
case 1:pfirst->athlete[i].score = 7;break;
case 2:pfirst->athlete[i].score = 5;break;
case 3:pfirst->athlete[i].score = 3;break;
case 4:pfirst->athlete[i].score = 2;break;
case 5:pfirst->athlete[i].score = 1;break;
}
}
if ( pfirst->athlete[i].item <=msp )
{
pfirst->menscore = pfirst->menscore + pfirst->athlete[i].score;
}
else
{
pfirst->womenscore = pfirst->womenscore + pfirst->athlete[i].score;
}
}
pfirst->totalscore = pfirst->menscore + pfirst->womenscore;
pfirst = pfirst->next;
}
}
void output()
{
pfirst = head->next;
psecond = head->next;
while ( pfirst->next != NULL )
{
// clrscr();
printf("\n第%d號學校的結果成績:",pfirst->serial);
printf("\n\n項目的數目\t學校的名字\t分數");
for (i=1;i<=ntsp;i++)
{
for (j=1;j<=pfirst->count;j++)
{
if ( pfirst->athlete[j].item == i )
{
printf("\n %d\t\t\t\t\t\t%s\n %d",i,pfirst->athlete[j].name,pfirst->athlete[j].score);break;
}
}
}
printf("\n\n\n\t\t\t\t\t\t按任意建 進入下一頁");
getchar();
pfirst = pfirst->next;
}
// clrscr();
printf("\n運動會結果:\n\n學校編號\t男運動員成績\t女運動員成績\t總分");
pfirst = head->next;
while ( pfirst->next != NULL )
{
printf("\n %d\t\t %d\t\t %d\t\t %d",pfirst->serial,pfirst->menscore,pfirst->womenscore,pfirst->totalscore);
pfirst = pfirst->next;
}
printf("\n\n\n\t\t\t\t\t\t\t按任意建結束");
getchar();
}
void create()
{
pfirst = (struct schoolstruct *)malloc(sizeof(struct schoolstruct));
pfirst->next = head->next ;
head->next = pfirst ;
pfirst->count = 1;
pfirst->menscore = 0;
pfirst->womenscore = 0;
pfirst->totalscore = 0;
}
void Save()
{FILE *fp;
if((fp = fopen("school.dat","wb"))==NULL)
{printf("can't open school.dat\n");
fclose(fp);
return;
}
fwrite(pfirst,sizeof(SCH),10,fp);
fclose(fp);
printf("文件已經成功保存\n");
}
void main()
{
system("cls");
printf("\n\t\t\t 運動會分數統計\n");
printf("輸入學校數目 (x>= 5):");
scanf("%d",&nsc);
printf("輸入男選手的項目(x<=20):");
scanf("%d",&msp);
printf("輸入女選手項目(<=20):");
scanf("%d",&wsp);
ntsp = msp + wsp;
phead = (int *)calloc(ntsp,sizeof(int));
pafirst = phead;
pasecond = phead;
input();
calculate();
output();
Save();
}
標簽:
源代碼
上傳時間:
2016-12-28
上傳用戶:150501