function [Xs idx_i idx_j idx_k Xma Xmb Xmc] = SamBaTen_initilize(X,sample_factor,fixed_set)
%Ekta Gujral,Ravdeep Pasricha , Vagelis Papalexakis 2017
%Computer Science and Engineering, University of California, Riverside
%Demonstrating the use of the SambaTen algorithm, using samplaing based
%approach for online tensors
if numel(sample_factor)>1
    s1 = sample_factor(1); 
    s2 = sample_factor(2);
    s3 = sample_factor(3);
else
    s1 = sample_factor; 
    s2 = sample_factor;
    s3 = sample_factor;
end

if nargin == 3
   fixed_i = fixed_set{1};
   fixed_j = fixed_set{2};
   fixed_k = fixed_set{3};
else
    fixed_i = [];
    fixed_j = [];
    fixed_k = [];
end
    
negative_values = find(X<0);
if ~isempty(negative_values)
   X_abs = double(X);
   X_abs = abs(X_abs);
   X_abs = sptensor(X_abs);
   X_abs=times(X_abs,X_abs);
else
   X_abs = times(X,X); 
end
s = size(X); I = s(1); J = s(2); K = s(3);

Xma = collapse(X_abs,[2 3]);
Xmb = collapse(X_abs,[1 3]);
Xmc = collapse(X_abs,[1 2]);

idx_i = find(Xma~=0);
idx_i = randweightedsample(idx_i,I/s1,full(Xma(idx_i)));
if ~isempty(fixed_i)
    idx_i = union(idx_i,fixed_i);
end
idx_j = find(Xmb~=0);
idx_j = randweightedsample(idx_j,J/s2,full(Xmb(idx_j)));
if ~isempty(fixed_j)
    idx_j = union(idx_j,fixed_j);
end
idx_k = find(Xmc~=0); % again
idx_k = randweightedsample(idx_k,K/s3,full(Xmc(idx_k))); % again
if ~isempty(fixed_k) % again
    idx_k = union(idx_k,fixed_k);       % again
end % again

Xs = X(idx_i,idx_j,idx_k);
