function W = onlineDescent(initial_W,dl, nc, svmbuff, ss, dataa, label,alpha)
%data_length = 3072;
%num_classes = 10;
%svm_buffer = 1;
%step_size = 0.0001;

data_length = dl;
num_classes = nc;
svm_buffer = svmbuff;
step_size = ss;
data = dataa;
labels = label;

W = initial_W;

%Load CIFAR-10 data
%load('data_batch_1.mat');
num_imgs = length(data);
loss_log = zeros(1,num_imgs);
for i = 1:num_imgs
    %Calculate score of image i
    img = im2double(data(i,:));
    score_i = W*img';
    
    %Get the label of image i
    y = double(labels(i));
    
    tmp = score_i' - score_i(y+1) + svm_buffer;
    maxed = max(0,tmp);
    loss = sum(tmp)-svm_buffer + alpha*norm(W); %Subtract loss contributed by the correct image
    loss_log(i) = loss;
    
    %Compute gradient
    bin_mask = find(maxed);
    bin_array = zeros(1,num_classes);
    bin_array(bin_mask) = 1;
    bin_array(y+1) = 0;
    
    gradient = bin_array' * img + alpha*2*W;
    
    W = W - step_size * gradient;
    
end
end