function [Trndata,Trn_lbs,trainTarget,y] = update_P(Trndata,Trn_lbs,data_p,buffer_p,preds_p,lblist,trainTarget,y,Bsize)


[n,d] = size(buffer_p);
max_n = Bsize;

num_bef = [];
for i =1:length(lblist)
    lb = lblist(i);
    num_Trn = sum(Trn_lbs==lb);
    num_bef = [num_bef,num_Trn];
end

trainTarget_1 = trainTarget(:,1:size(Trndata,1));
trainTarget_2 = trainTarget(:,size(Trndata,1)+1:end);
y_1 = y(:,1:size(Trndata,1));
y_2 = y(:,size(Trndata,1)+1:end);

 for i =1:length(lblist)
     lb = lblist(i);
     num_Trn = sum(Trn_lbs==lb);
     if num_Trn==max_n
         continue;
     end
     if num_Trn>max_n
         idx = 1:size(Trndata,1);
         idx = idx(1,Trn_lbs==lb);
         del_seq = idx(1,max_n+1:length(idx));
         Trndata(del_seq',:) = [];
         Trn_lbs(del_seq) = [];
         trainTarget_1(:,del_seq) = [];
         y_1(:,del_seq) = [];
     else
         add_n = max_n-num_Trn;
         num_pred = sum(preds_p==lb);
         add_n = min(add_n,num_pred);
         idx = 1:size(data_p,1);
         idx_select = idx(1,preds_p==lb);
         add_seq = idx_select(1,1:add_n);
         data_add = data_p(add_seq',:);
         Trndata = [Trndata;data_add];
         Trn_lbs = [Trn_lbs,repmat(lb,1,add_n)];
         
         target_add = trainTarget_2(:,add_seq);
         trainTarget_1 = [trainTarget_1,target_add];
         y_1 = [y_1,y_2(:,add_seq)];
     end
 end

trainTarget = trainTarget_1;
y = y_1;
 
end