Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help to vectorize for loops and make code faster

Subject: Help to vectorize for loops and make code faster

From: Aidy

Date: 10 Apr, 2011 08:22:04

Message: 1 of 1

Hi everyone,

I created this function at work today. It is very very slow. I would like some help in making a Matlab function faster. I have several for loops , that drastically slows down my program. Can anyone please help me in vectorizing the code if it is possible.

The function is a bit long .You can download my function at the following link :

http://www.yourfilelink.com/get.php?fid=648747

It is also posted below :
thanks



%--------------------------------------------------------------------------------------------------
function classifier = trainClassifier(positiveExamples, negativeExamples, filters)

T = 40 % number of classifiers to pick

% other variables
numPos = size(positiveExamples,2);
numNeg = size(negativeExamples,2);
numExamples = numPos+numNeg;
%numFilters = size(filters,2);
numFilters = sum(filters(:,1)); % column f

% Format the dataset better
X = [positiveExamples negativeExamples];
Y = [ones(1,numPos) zeros(1,numNeg)];

% Initialize weights for the dataset
W = 0.5*[repmat(1/numPos,1,numPos) repmat(1/numNeg,1,numNeg)];

% compute filter lookup table (to map filter index to filters)
filterMap = computeFilterMap(filters);

% compute raw responses
%R = zeros(numFilters,numExamples);
%for currentEx = 1:numExamples
% diffI = X{currentEx};
% R(:,currentEx) = filterResponseDiffI(filters,diffI)';
%end
maxR = 2000;
R = zeros(maxR,numExamples);

fprintf('\n ');
classifier = zeros(0,10);
for t = 1:T
  fprintf('\10\10\10\10\10\10\10\10\10\10\10\10\10');
  fprintf('...%0.3d/%0.3d...',t,T);
  
  % normalize the weights (these are weights for the inputs, not the features!)
  W = W / sum(W);
  
  bestError = inf;
  % train the weak classifiers over the weighted inputs
  for i = 1:numFilters
    
    RIndex = mod(i-1,maxR)+1;
    if RIndex == 1
        % fill in R for this section
        i2 = min(i+maxR-1,numFilters);
        filters_ = filters(filterMap(i,1):filterMap(i2,2),:);
        for currentEx = 1:numExamples
            diffI = X{currentEx};
            R(1:i2-i+1,currentEx) = filterResponseDiffI(filters_,X{currentEx})';
        end
    end
        
    % use univariate quadratic discriminant analysis as done by Satya (who got it from Duda)
    Wp = W(1:numPos); Wn = W(numPos+1:numExamples);
    Rp = R(RIndex,1:numPos); Rn = R(RIndex,numPos+1:numExamples);
    
    mean_p = Wp*Rp' / sum(Wp);
    %var_p = Wp*( (Rp-repmat(mean_p,1,numPos)).^2 )' / sum(Wp);
    mean_n = Wn*Rn' / sum(Wn);
    %var_n = Wn*( (Rn-repmat(mean_n,1,numNeg)).^2 )' / sum(Wn);
    
    % XXX : Hack! This is not the right way to do it; I'm making the threshold the midpoint of mean_p and mean_n
    threshold = (mean_p + mean_n)/2;
    
    % calculate error
    posResponses = R(RIndex,:) >= threshold;

    % xor with Y to determine misclassifications
    misclassifications = xor(posResponses,Y);
    
    % dot product with W to get error
    error = W * misclassifications';
    
    if (error < bestError)
      bestFeatureIndex = i;
      bestThreshold = threshold;
      bestError = error;
      bestMisclassifications = misclassifications;
    end
  end
  
  % update the weights
  beta = bestError / (1 - bestError);
  W = W .* beta.^(1-bestMisclassifications);
  alpha(t) = log(1/beta);

  filter = filters(filterMap(bestFeatureIndex,1):filterMap(bestFeatureIndex,2),:);
  filterLength = size(filter,1);
  feature = [filter zeros(filterLength,4)];
  feature(filterLength,7) = bestThreshold;
  feature(filterLength,8) = alpha(t);
  classifier(size(classifier,1)+1:size(classifier,1)+filterLength,:) = feature;

end

fprintf('\n');

classifier(size(classifier,1),9) = 1;
classifier(size(classifier,1),10) = sum(alpha)/2; % threshold

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us