Convert this C++ code to MATLAB
2 次查看(过去 30 天)
显示 更早的评论
Hi. Kindly can you help in converting this C++ code to MATLAB
Listing 9.1 fuzzyam.h
//fuzzyam.h V. Rao, H. Rao
#include <iostream.h>
#define MXSIZ 10
class fzneuron
{
protected:
int nnbr;
int inn,outn;
float output;
float activation;
float outwt[MXSIZ];
char *name;
friend class network;
public:
fzneuron() { };
void getnrn(int,int,int,char *);
};
class exemplar
{
protected:
int xdim,ydim;
float v1[MXSIZ],v2[MXSIZ]; // this is different from BAM
friend class network;
public:
exemplar() { };
void getexmplr(int,int,float *,float *);
void prexmplr();
};
class asscpair
{
protected:
int xdim,ydim,idn;
float v1[MXSIZ],v2[MXSIZ];
friend class network;
public:
asscpair() { };
void getasscpair(int,int,int);
void prasscpair();
};
class potlpair
{
protected:
int xdim,ydim;
float v1[MXSIZ],v2[MXSIZ];
friend class network;
public:
potlpair() { };
void getpotlpair(int,int);
void prpotlpair();
};
class network
{
public:
int anmbr,bnmbr,flag,nexmplr,nasspr,ninpt;
fzneuron (anrn)[MXSIZ],(bnrn)[MXSIZ];
exemplar (e)[MXSIZ];
asscpair (as)[MXSIZ];
potlpair (pp)[MXSIZ];
float outs1[MXSIZ],outs2[MXSIZ]; // change from BAM to floats
double mtrx1[MXSIZ][MXSIZ],mtrx2[MXSIZ][MXSIZ]; // change from
BAM to doubles
network() { };
void getnwk(int,int,int,float [][6],float [][4]);
void compr1(int,int);
void compr2(int,int);
void prwts();
void iterate();
void findassc(float *);
void asgninpt(float *);
void asgnvect(int,float *,float *);
void comput1();
void comput2();
void prstatus();
};
Source File
Listing 9.2 fuzzyam.cpp
//fuzzyam.cpp V. Rao, H. Rao
#include "fuzzyam.h"
float max(float x,float y) //new for FAM
{
float u;
u = ((x>y) ? x : y );
return u;
}
float min(float x,float y) // new for FAM
{
float u;
u =( (x>y) ? y : x) ;
return u;
}
void fzneuron::getnrn(int m1,int m2,int m3,char *y)
{
int i;
name = y;
nnbr = m1;
outn = m2;
inn = m3;
for(i=0;i<outn;++i){
outwt[i] = 0 ;
}
output = 0;
activation = 0;
}
void exemplar::getexmplr(int k,int l,float *b1,float *b2) // changed
from BAM
{
int i2;
xdim = k;
ydim = l;
for(i2=0;i2<xdim;++i2){
v1[i2] = b1[i2]; }
for(i2=0;i2<ydim;++i2){
v2[i2] = b2[i2]; }
}
void exemplar::prexmplr()
{
int i;
cout<<"\nX vector you gave is:\n";
for(i=0;i<xdim;++i){
cout<<v1[i]<<" ";}
cout<<"\nY vector you gave is:\n";
for(i=0;i<ydim;++i){
cout<<v2[i]<<" ";}
cout<<"\n";
}
void asscpair::getasscpair(int i,int j,int k)
{
idn = i;
xdim = j;
ydim = k;
}
void asscpair::prasscpair()
{
int i;
cout<<"\nX vector in the associated pair no. "<<idn<<"is:\n";
for(i=0;i<xdim;++i){
cout<<v1[i]<<" ";}
cout<<"\nY vector in the associated pair no. "<<idn<<"is:\n";
for(i=0;i<ydim;++i){
cout<<v2[i]<<" ";}
cout<<"\n";
}
void potlpair::getpotlpair(int k,int j)
{
xdim = k;
ydim = j;
}
void potlpair::prpotlpair()
{
int i;
cout<<"\nX vector in possible associated pair is:\n";
for(i=0;i<xdim;++i){
cout<<v1[i]<<" ";}
cout<<"\nY vector in possible associated pair is:\n";
for(i=0;i<ydim;++i){
cout<<v2[i]<<" ";}
cout<<"\n";
}
void network::getnwk(int k,int l,int k1,float b1[][6],float
b2[][4])
{
anmbr = k;
bnmbr = l;
nexmplr = k1;
nasspr = 0;
ninpt = 0;
int i,j,i2;
float tmp1,tmp2;
flag =0;
char *y1="ANEURON", *y2="BNEURON" ;
for(i=0;i<nexmplr;++i){
e[i].getexmplr(anmbr,bnmbr,b1[i],b2[i]);
e[i].prexmplr();
cout<<"\n";
}
for(i=0;i<anmbr;++i){
anrn[i].fzneuron::getnrn(i,bnmbr,0,y1);}
for(i=0;i<bnmbr;++i){
bnrn[i].fzneuron::getnrn(i,0,anmbr,y2);}
for(i=0;i<anmbr;++i){
for(j=0;j<bnmbr;++j){
tmp1 = 0.0;
for(i2=0;i2<nexmplr;++i2){
tmp2 = min(e[i2].v1[i],e[i2].v2[j]);
tmp1 = max(tmp1,tmp2);
}
mtrx1[i][j] = tmp1;
mtrx2[j][i] = mtrx1[i][j];
anrn[i].outwt[j] = mtrx1[i][j];
bnrn[j].outwt[i] = mtrx2[j][i];
}
}
prwts();
cout<<"\n";
}
void network::asgninpt(float *b)
{
int i,j;
cout<<"\n";
for(i=0;i<anmbr;++i){
anrn[i].output = b[i];
outs1[i] = b[i];
}
}
void network::compr1(int j,int k)
{
int i;
for(i=0;i<anmbr;++i){
if(pp[j].v1[i] != pp[k].v1[i]) flag = 1;
break;
}
}
void network::compr2(int j,int k)
{
int i;
for(i=0;i<anmbr;++i){
if(pp[j].v2[i] != pp[k].v2[i]) flag = 1;
break;}
}
void network::comput1() //changed from BAM
{
int j;
for(j=0;j<bnmbr;++j){
int ii1;
float c1 =0.0,d1;
cout<<"\n";
for(ii1=0;ii1<anmbr;++ii1){
d1 = min(outs1[ii1],mtrx1[ii1][j]);
c1 = max(c1,d1);
}
bnrn[j].activation = c1;
cout<<"\n output layer neuron "<<j<<" activation is "
<<c1<<"\n";
bnrn[j].output = bnrn[j].activation;
outs2[j] = bnrn[j].output;
cout<<"\n output layer neuron "<<j<<" output is "
<<bnrn[j].output<<"\n";
}
}
void network::comput2() //changed from BAM
{
int i;
for(i=0;i<anmbr;++i){
int ii1;
float c1=0.0,d1;
for(ii1=0;ii1<bnmbr;++ii1){
d1 = min(outs2[ii1],mtrx2[ii1][i]);
c1 = max(c1,d1);}
anrn[i].activation = c1;
cout<<"\ninput layer neuron "<<i<<"activation is "
<<c1<<"\n";
anrn[i].output = anrn[i].activation;
outs1[i] = anrn[i].output;
cout<<"\n input layer neuron "<<i<<"output is "
<<anrn[i].output<<"\n";
}
}
void network::asgnvect(int j1,float *b1,float *b2)
{
int j2;
for(j2=0;j2<j1;++j2){
b2[j2] = b1[j2];}
}
void network::prwts()
{
int i3,i4;
cout<<"\n weights−− input layer to output layer: \n\n";
for(i3=0;i3<anmbr;++i3){
for(i4=0;i4<bnmbr;++i4){
cout<<anrn[i3].outwt[i4]<<" ";}
cout<<"\n"; }
cout<<"\n";
cout<<"\nweights−− output layer to input layer: \n\n";
for(i3=0;i3<bnmbr;++i3){
for(i4=0;i4<anmbr;++i4){
cout<<bnrn[i3].outwt[i4]<<" ";}
cout<<"\n"; }
cout<<"\n";
}
void network::iterate()
{
int i1;
for(i1=0;i1<nexmplr;++i1){
findassc(e[i1].v1);
}
}
void network::findassc(float *b)
{
int j;
flag = 0;
asgninpt(b);
ninpt ++;
cout<<"\nInput vector is:\n" ;
for(j=0;j<6;++j){
cout<<b[j]<<" ";};
cout<<"\n";
pp[0].getpotlpair(anmbr,bnmbr);
asgnvect(anmbr,outs1,pp[0].v1);
comput1();
if(flag>=0){
asgnvect(bnmbr,outs2,pp[0].v2);
cout<<"\n";
pp[0].prpotlpair();
cout<<"\n";
comput2(); }
for(j=1;j<MXSIZ;++j){
pp[j].getpotlpair(anmbr,bnmbr);
asgnvect(anmbr,outs1,pp[j].v1);
comput1();
asgnvect(bnmbr,outs2,pp[j].v2);
pp[j].prpotlpair();
cout<<"\n";
compr1(j,j−1);
compr2(j,j−1);
if(flag == 0) {
int j2;
nasspr += 1;
j2 = nasspr;
as[j2].getasscpair(j2,anmbr,bnmbr);
asgnvect(anmbr,pp[j].v1,as[j2].v1);
asgnvect(bnmbr,pp[j].v2,as[j2].v2);
cout<<"\nPATTERNS ASSOCIATED:\n";
as[j2].prasscpair();
j = MXSIZ ;
}
else
if(flag == 1)
{
flag = 0;
comput1();
}
}
}
void network::prstatus()
{
int j;
cout<<"\nTHE FOLLOWING ASSOCIATED PAIRS WERE FOUND BY FUZZY AM\n\n";
for(j=1;j<=nasspr;++j){
as[j].prasscpair();
cout<<"\n";}
}
void main()
{
int ar = 6, br = 4, nex = 1;
float inptv[][6]={0.1,0.3,0.2,0.0,0.7,0.5,0.6,0.0,0.3,0.4,0.1,0.2};
float outv[][4]={0.4,0.2,0.1,0.0};
cout<<"\n\nTHIS PROGRAM IS FOR A FUZZY ASSOCIATIVE MEMORY NETWORK. THE
NETWORK \n";
cout<<"IS SET UP FOR ILLUSTRATION WITH "<<ar<<" INPUT NEURONS, AND "<<br;
cout<<" OUTPUT NEURONS.\n"<<nex<<" exemplars are used to encode \n";
static network famn;
famn.getnwk(ar,br,nex,inptv,outv);
famn.iterate();
famn.findassc(inptv[1]);
famn.prstatus();
}
1 个评论
yahay
2022-11-30
can you help in converting this C++ code to MATLAB
#include <stdio.h>
#include <iostream>
using namespace std;
void linearSearch(int a[], int n) {
int temp = -1;
for (int i = 0; i < 8; i++) {
if (a[i] == n) {
cout << "Element found at position: " << i + 1 << endl;
temp = 0;
break;
}
}
if (temp == -1) {
cout << "No Element Found" << endl;
}
}
int main() {
int arr[8];
cout << "Please enter 8 elements of the Array" << endl;
for (int i = 0; i < 8; i++) {
cin >> arr[i];
}
cout << "Please enter an element to search" << endl;
int num;
cin >> num;
linearSearch(arr, num);
return 0;
}
回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!