Saturday, August 20, 2011

C/C++ map [Object type]

class info
{
    char infoLine[MAXLINELEN];
    char infoMark[NOSPECIES];
    int count;

public:

    //info(){  strcpy(infoLine,""); strcpy(infoMark,""); for(int i=0;i

    // for new  entry
    info(char* li, int index, char mr){

        strcpy(infoLine,li);
        //cout << infoLine << endl;
        for(int i=0;i
            infoMark[i]='+' ;
        infoMark[index]=mr;
        count = 1;
    }

    // for existing entry
    void insertmark(int index, char currentMark) { infoMark[index]=currentMark; count++;  }
    void putEndmark(int index)
    {
        infoMark[index]='\0' ;
    }
    int getCount()  {return count;}
    char *getLine() {return infoLine; }
    char *getMark() {return infoMark; }

};


map mymap; // it must be string . otherwise you have to handle < operator overload in info object
map::iterator it;


it = mymap.find(ID);
if ( it !=mymap.end() ) // found id
        {
           it->second.insertmark( specNo-1,'-');
        }else // not found
        {
           mymap.insert( pair  ( ID,info(line2,specNo-1,'-')  )  );
        }



C/C++ map [primitive type]

void testMap()
{

    map mymap;
    map::iterator rit;

    mymap["x1"] = 100;
    mymap.insert(pair("y2",150));

    mymap["y2"] = 200;
    mymap["y2"] = 400; // this value will replace previous one.


    rit = mymap.find("xx");
    if ( rit !=mymap.end() ) // found id
    {
        rit->second= 11111; // change existing content
    }else // not found
    {

        mymap.insert( pair("xx",150)    );
    }
    // show content:
    for ( rit=mymap.begin() ; rit != mymap.end(); rit++ )
        cout << rit->first << " => " << rit->second << endl;

}

Tuesday, August 16, 2011

MATLAB som

% 1 4 5 7 -- sample 1
% 2 5 6 6 -- sample 2

% in cndx = you will get  the position of each training sample in cluster no
% as SOM takes each sample as a column we need to transpose feature before applying on SOM

%  -------  train -----------
trainFeature = [ 1 2  ; 4 5    ; 5 6 ; 7 6 ]; % Two training data with 4 feature each . So each column is a sample
net = newsom(  trainFeature'  ,[2 2] ); % 4 cluster
net.trainParam.epochs = 100;
[net,tr,Y,E,Pf,Af] = train(net, trainFeature'  );
distances = dist( trainFeature  ,net.IW{1}' );
[d,cndx_train ] = min(distances,[],2); % cndx gives the cluster index, d gives the distance
cndx_train % the position of each training sample in cluster no


bookKeep=zeros(SOMd1 * SOMd2 ,2);%one column for +ve,one column for -ve
    for c=1: totTrain
        index = cndx_train(c);
        if(c<=totPosTrain)
            bookKeep( index,1) = bookKeep( index,1) + 1; %+ve sample
        else
            bookKeep( index,2) = bookKeep( index,2) + 1; %-ve sample
        end
    end
    dlmwrite('clusterInfo',bookKeep,'\t');

% ---------  test  -------------

out = sim(net, featureInTest' );
% format: clusterNo*testSample : 0 0 0 1 0 0 0 0 % that means 4th sample in this custer;
% outFormat (clusterNo, serial )

     out = out'; % #testSample * clusterNo 
    dlmwrite('out',out,'\t');
    for c=1:totTest
      
        clusterNo(c) = find( out(c ,:) );     
        if c<=totPosTest
            origLabel(c) = 1;
        else
            origLabel(c) = -1;
        end
      
    end
    dlmwrite('predicted',[origLabel' clusterNo'],'\t');

MATLAB read random line if line size is fixed

NUM_EXAMPLE = 6;
NOOFTRAINING = 6;
NOOFCHARPERLINE = 10 ;
NOOFOFFSETPERLINE = NOOFCHARPERLINE+ 2; % sometimes it maybe 2 depending upon whow data was written into file


fid = fopen( 'D:\KAUST\2.Winter2010-11\SpliceSites\test.txt','r');


for i=1: NOOFTRAINING
   
   rowno = round(rand(1)*NUM_SAMPLE) ;
  
   offset = (rowno - 1 ) * NOOFOFFSETPERLINE;
   fseek(fid,offset,'bof');
   line = fgetl(fid);
   frewind(fid);
  
   disp(rowno);
%    disp(offset);
   disp(line);
  
end

fclose(fid);

MATLA entropy calculation

X = [ .5 .5];
lX = log2(X);
ent = -(X * lX' )

MATLAB read excel file

Basic Mode :If microsoft Excel is not installed
=============================

1. Excel file must be saved in win95 format. 97/98/2000 may not work.

2. Can not read range. Read whole file.

 

    %  or read the first sheet
   ---------------------------------------

    all = xlsread( 'filename' );


    %  or read the specified sheet
   ---------------------------------

all = xlsread( 'filename','sheetname','' , 'basic'  )

MATLAB neural network

2010a
================
net=newff(trainFeature,trainValue,[8 1],{'tansig' 'purelin'});

2009a
===============

net=newff(trainFeature,trainValue,[8 1]);
net.layers{1}.transferFcn = 'tansig';
net.layers{2}.transferFcn = 'purelin'


save net
==================
 save net;

%-------------------- Radial Basis Network No train -------------------

  net=newrb(trainFeature,trainValue,0.0, 1, 100, 1);
 save net;

NO TRAIN . IT WILL AUTOMATICALLY CRATE NEW NEURON


load net for testing
====================

load net;

MATLAB adding noise into data


add percent  % noise to each value
====================================
percent=.03;

sample = sampleMatrix(index, :);
noiseSample=[];
for i=1 :instanceSize
       val = sample(i);
       smallPerc = val * percent ; % 1 percent noise add
       noise = smallPerc* [ rand(1) - 0.5 ]; % .*T
       noisyVal = val + noise ; % small is very small 10e-9
       noiseSample = [noiseSample noisyVal];
end
noiseMatrix = [ noiseMatrix ; noiseSample];

Add little noise to X
======================

rand( 'state', 0 ) % any number for random number
randn('state', 0 ) % any number for random number
small = 10e-11
small* [ rand(size(X)) - .5 ] .* X + X  % small is very small 10e-9

index = ceil(a + (b-a).*rand(1) ); %

Reading file in C


void readLine()
{
    FILE *fpTest;
    fpTest = fopen ( "test.txt", "r" );
    int countRead = 0,countLine = 0,countCodeDataLine = 0,length = 0;
    if(fpTest ==NULL){
        printf("error in opening test.txt file" ); // perror
        exit(0);
    }
   
   
    while( !feof(fpTest)    )
    {
        fgets ( line, MAXATTRLINELEN, fpTest );
        countRead++;
        length = strlen(line);
        printf("[%d]:%s(lineLength=%d) \n",countLine,line,length);
       
        if(length > 0)
        {
            countLine++;
        }
       
        // count countCodeDataLine
        if(length == 0) ;
        else if(length ==1)
        {
            //printf(" %d  %d %d %d %d",line[0], '\n', '\r' , '\r\n', '\n\r');
            if(line[0]==' ' || line[0] == '\n'  || line[0] == '\t' ) ;
            else
                countCodeDataLine++;
        }else
        {
            countCodeDataLine++;   
        }
       
        line[0] = '\0';
       

    }
   
   
    printf(" Read %d . Line  %d . Dataline  %d\n",countRead, countLine,countCodeDataLine);

    fclose ( fpTest );
   
}

Dynamic array in C/C++


Two dim float malloc
====================

    float ** PC;

    PC = (float**) malloc(DIM_PC_X * sizeof(float*)); // [DIM_PC_X][DIM_PC_Y];
    for(int i = 0; i < DIM_PC_X; i++)
        PC[i] = (float*) malloc(DIM_PC_Y * sizeof(float));



Two dim float using new
============================

int sizeX=5,sizeY = 2;
   
int** ary = new int*[sizeX];
   
for(int i = 0; i < sizeX; ++i)
       
    ary[i] = new int[sizeY];

Running openmp in eclipse

As we know to run openmp in gcc , C++ project we have to compile it with

g++ -fopenmp option. To configure this with eclipse you just need to add -fopenmp under GCC C++ linker command option