这是一种执行您要求的方法(尽管您的问题中没有明确定义某些情况):
% 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
,那么我们添加与 cen 一样多的行,直到我们到达rows_limit
然后循环退出。
所以对于 a rows_limit = 4;
,你会得到newT
:
memberId productId Rating
________ _________ ______
'B' 'x' 2
'B' 'w' 4
'B' 't' 2
'A' 'x' 5
只要用户之间存在联系,因此每个用户的相关产品都会将新用户带到列表中,循环会随着新用户继续在newT
. 但是,我们可能从一个节点开始,并非所有其他节点都是其网络的一部分。例如,看一下下图,它说明了我在上面的代码中使用的扩展示例中的连接:

NodeD
没有与所有其他人连接,所以除非我们积极地在 中寻找新的不相关用户,否则我们T
永远不会找到它。上面的实现确实寻找这种用户。