Matlab parse a file in an array of cells

I have a file in the following format in matlab:

user_id_a: (item_1,rating),(item_2,rating),...(item_n,rating)
user_id_b: (item_25,rating),(item_50,rating),...(item_x,rating)
....
....

therefore, each line has values ​​separated by a colon, where the value to the left of the colon is a number representing user_id, and the values ​​to the right are tuples item_ids (also numbers) and rating (numbers do not float).

I would like to read this data in an array of Matlab cells or better, but ultimately convert it to a sparse matrix, where user_id represents the index of the row and item_id represents the index of the column and stores the corresponding rating in that index of the array. (This will work because I know a priori the number of users and elements in my universe, so identifiers cannot be larger than this).

Any help would be appreciated.

I have so far tried the textscan function as follows:

c = textscan(f,'%d %s','delimiter',':')   %this creates two cells one with all the user_ids
                                          %and another with all the remaining string values.

, - str2mat(c{2}), , '(' ')' . , .

Matlab .

+3
2
f = fopen('data.txt','rt'); %// data file. Open as text ('t')
str = textscan(f,'%s'); %// gives a cell which contains a cell array of strings
str = str{1}; %// cell array of strings
r = str(1:2:end);
r = cellfun(@(s) str2num(s(1:end-1)), r); %// rows; numeric vector
pairs = str(2:2:end); 
pairs = regexprep(pairs,'[(,)]',' ');
pairs = cellfun(@(s) str2num(s(1:end-1)), pairs, 'uni', 0);
%// pairs; cell array of numeric vectors
cols = cellfun(@(x) x(1:2:end), pairs, 'uni', 0);
%// columns; cell array of numeric vectors
vals = cellfun(@(x) x(2:2:end), pairs, 'uni', 0);
%// values; cell array of numeric vectors
rows = arrayfun(@(n) repmat(r(n),1,numel(cols{n})), 1:numel(r), 'uni', 0);
%// rows repeated to match cols; cell array of numeric vectors
matrix = sparse([rows{:}], [cols{:}], [vals{:}]);
%// concat rows, cols and vals into vectors and use as inputs to sparse

1: (1,3),(2,4),(3,5)
10: (1,1),(2,2)

:

matrix =
   (1,1)        3
  (10,1)        1
   (1,2)        4
  (10,2)        2
   (1,3)        5
+1

, Matlab stringsplit, , , . userid " ", , , .

( , - - , , ). \s* , , . - , .

% matlab_test.txt:
% 101: (1,42),(2,65),(5,0)
% 102: (25,78),(50,12),(6,143),(2,123)
% 103: (23,6),(56,3)

clear all;
fclose('all');
% your path will vary, of course
file = '<path>/matlab_test.txt';
f = fopen(file);
c = textscan(f,'%d %s','delimiter',':');
celldisp(c)
uids = c{1}
tuples = c{2}

% These are stated as known
num_users = 3;
num_items = 40;

desired_array = zeros(num_users, num_items);
expression = '\((\d+)\s*,\s*(\d+)\)'
% Assuming length(tuples) == num_users for simplicity
for k = 1:num_users
    uid = uids(k)
    tokens = regexp(tuples{k}, expression, 'tokens');
    for l = 1:length(tokens)
        item_id = str2num(tokens{l}{1})
        rating = str2num(tokens{l}{2})
        desired_array(uid, item_id) = rating;
    end
end
0

All Articles