将表的多行添加到另一个表

时间:2016-09-27 10:57:18

标签: matlab matlab-table

我有一张如下表格:(这是我桌子上的几行)

T = table({'A';'A';'A';'B';'B';'B';'C';'C';'C';'C'}, {'x';'y';'z';'x';'w';'t';'z';'x';'t';'o'},[5;1;2;2;4;2;2;5;4;1], ...
      'VariableNames', {'memberId', 'productId','Rating'});

T:

A  x  5
A  y  1
Z  z  2
B  x  2
B  w  4
B  t  2
C  z  2
C  x  5
C  t  4
C  o  1
C  u  3
D  r  1
D  t  2
D  w  5
.
.
.
.

我需要用户A然后创建一个像Previous表(表T)这样的表,并且所有行都与用户A相关以进入该表。表中的这一点是以下几行:

A  x  5
A  y  1
A  z  2

接下来,考虑与该用户相关的产品,即x,y,z。然后包含x然后y和z的所有行都添加到表中。在表格的这一点上有以下几行:

A  x  5
A  y  1
A  z  2
B  x  2
C  z  2
C  x  5

然后,其他用户已被添加到表中以考虑即B,C。然后为第一个用户做了同样的事情(A)为这个用户完成(分别为B然后是C)。 这样做是为了在表中添加所需的行数。这里,例如,需要8行。即最终结果如下:

A  x  5
A  y  1
A  z  2
B  x  2
C  z  2
C  x  5
B  w  4
B  t  2

即工作完成时,要导入的第二个表行中请求的行数。

如果有任何机构在这方面帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一种做你要求的方式(虽然有些案例在你的问题中没有明确定义):

% I added user 'D' for the scenario of an unconnected node
T = table({'A';'A';'A';'B';'B';'B';'C';'C';'C';'C';'D';'D';'D';'D'},...
    {'x';'y';'z';'x';'w';'t';'z';'x';'t';'o';'q';'p';'f';'v'},...
    [5;1;2;2;4;2;2;5;4;1;4;5;2;1], ...
    'VariableNames', {'memberId', 'productId','Rating'});
% initial preparations:
rows_limit = 8;
first_user = 'B'; % this is just for readability
newT = table(cell(rows_limit,1),cell(rows_limit,1),zeros(rows_limit,1),...
    'VariableNames',{'memberId', 'productId','Rating'});
% We need an index vector so we won't add the same row twice:
added = false(height(T),1);
row_count = 1;
users_list = {first_user};

% now we start adding rows to newT until it's full: 
while row_count<rows_limit
    while numel(users_list)>=1
        % get all the user's rows
        next_to_add = strcmp(T.memberId,users_list{1}) & ~added;
        % if this user has any rows to be added:
        if sum(next_to_add)>0
            % if there's enough empty rows in newT add them to it:
            if  sum(next_to_add) <= rows_limit-row_count+1
                newT(row_count:row_count+sum(next_to_add)-1,:) = T(next_to_add,:)
                % and update the index vector:
                added = added | strcmp(T.memberId,users_list{1});
            else
                % otherwise - fill the empty rows and quit the loop:
                if row_count <= rows_limit
                    end_to_add = find(next_to_add,rows_limit-row_count+1);
                    newT(row_count:rows_limit,:) = T(end_to_add,:)
                end
                row_count = rows_limit+1; % to exit the outer loop
                break
            end
            row_count = row_count+sum(next_to_add);

            % Add related products:
            % ====================
            % save the first new user to be addaed by related products:
            last_user_row = row_count;
            % get all the products we already added to newT:
            products = unique(newT.productId(1:row_count-1),'stable');
            % although we want only the last user products, because we add all the
            % products the before, our index vector ('added') will eliminate them
            for p = 1:numel(products)
                % get all the product's rows
                next_to_add = strcmp(T.productId,products{p}) & ~added;
                % if there's enough empty rows in newT add them to it:
                if sum(next_to_add)>0
                    if sum(next_to_add) <= rows_limit-row_count+1
                        newT(row_count:row_count+sum(next_to_add)-1,:) = T(next_to_add,:);
                        % and update the index vector:
                        added = added | strcmp(T.productId,products{p});
                    else
                        % otherwise - fill the empty rows and quit the loop:
                        if row_count <= rows_limit
                            end_to_add = find(next_to_add,rows_limit-row_count+1);
                            newT(row_count:rows_limit,:) = T(end_to_add,:);
                        end
                        row_count = rows_limit+1; % to exit the outer loop
                        break
                    end
                end
                row_count = row_count+sum(next_to_add);
            end
        end
        % get the list of new users we just added, and concat to the users
        % left in the original list:
        users_list = [unique(newT.memberId(last_user_row:row_count-1),'stable');
            unique(T.memberId(~added),'stable')];
    end
end

给出了newT

memberId    productId    Rating
________    _________    ______
'B'         'x'          2     
'B'         'w'          4     
'B'         't'          2     
'A'         'x'          5     
'C'         'x'          5     
'C'         't'          4     
'A'         'y'          1     
'A'         'z'          2     

在此实现中,行逐个用户添加,逐个产品添加,如果要添加的下一个用户/产品有更多行,那么newT中可用的行,则添加尽可能多的行我们岑,直到我们到达rows_limit然后循环退出。

因此对于rows_limit = 4;,您将获得newT

memberId    productId    Rating
________    _________    ______
'B'         'x'          2     
'B'         'w'          4     
'B'         't'          2     
'A'         'x'          5     

只要用户之间存在连接,因此每个用户的相关产品都会将新用户带到列表中,循环将继续newT中的新用户。但是,它可能是我们从一个节点开始,并非所有其他节点都是其网络的一部分。例如,看一下下面的图形图,说明我在上面的代码中使用的扩展示例中的连接:

users_net

节点D未与所有其他节点相关联,因此,除非我们在T中主动寻找新的无关用户,否则我们永远不会接受它。上面的实现确实在寻找这种用户。