Using forloop and whileloop to find if a number has appeared repetitively

1 回表示 (過去 30 日間)
Rui Xu
Rui Xu 2021 年 11 月 30 日
コメント済み: Dyuman Joshi 2021 年 11 月 30 日
This is a 13 x 2 vector where the first number is associated with a movie ID, and the second column is a actor ID who acted in this movie. Is there a way that I can compile 5698, 5699, 5700 (for example, I want it to be 5698 - 79543 80537 73975 79604). Would I need to a for loop and a while loop to do that? Something like:
for ii = 1:size(vector, 1)
while vector(ii) == vector(ii+1) ...
The goal of this is to check if the movie is an isolated, in which all of its actor has only acted in this movie, instead of appearing in other movies as well. For right now, I need to check all the actors associated with the same movie ID, and if they all have not appeared in other movies, then I would store this movie ID in an array.
5698 79543
5698 80537
5698 73975
5698 79604
5699 1348
5699 29926
5699 10084
5699 80351
5700 80093
5700 80092
5700 73392
5700 73579
5700 74767

採用された回答

Dyuman Joshi
Dyuman Joshi 2021 年 11 月 30 日
You can also use groupsummary but since you mentioned loops, here a way with for loop -
Since the size of the groups differ, it's not possible to store them in a numeric array. You can save them in a cell array.
data = [5698 79543; 5698 80537; 5698 73975; 5698 79604; 5699 1348; 5699 29926; 5699 10084; 5699 80351; 5700 80093; 5700 80092; 5700 73392; 5700 73579; 5700 74767];
y = unique(data(:,1));
for i=1:numel(y)
movie = y(i)
actors = data(find(data(:,1)==y(i)),2)
end
movie = 5698
actors = 4×1
79543 80537 73975 79604
movie = 5699
actors = 4×1
1348 29926 10084 80351
movie = 5700
actors = 5×1
80093 80092 73392 73579 74767
  4 件のコメント
Awais Saeed
Awais Saeed 2021 年 11 月 30 日
You can use struct or cell array for this
Dyuman Joshi
Dyuman Joshi 2021 年 11 月 30 日
"Since the size of actors is changing, is it possible to do a forloop to go through each of the elements in actors to check for repetition in another database?"
You can use unique() for them as well. If you want to save them, there are multiple ways (not sure which one you prefer)
data = [5698 79543; 5698 80537; 5698 73975; 5698 79604; 5699 1348; 5699 29926; 5699 10084; 5699 80351; 5700 80093; 5700 80092; 5700 73392; 5700 73579; 5700 74767];
y = unique(data(:,1));
for i=1:numel(y)
z1{i,1} = y(i);
z1{i,2} = data(find(data(:,1)==y(i)),2);
end
z1
z1 = 3×2 cell array
{[5698]} {4×1 double} {[5699]} {4×1 double} {[5700]} {5×1 double}
for i=1:numel(y)
z2{i} = [y(i) data(find(data(:,1)==y(i)),2)'];
end
z2
z2 = 1×3 cell array
{[5698 79543 80537 73975 79604]} {[5699 1348 29926 10084 80351]} {[5700 80093 80092 73392 73579 74767]}

サインインしてコメントする。

その他の回答 (1 件)

Awais Saeed
Awais Saeed 2021 年 11 月 30 日
Doing with loops is hectic. You should use find(). However, I did it with loops as you required. I added last two entires for my verification.
list = [5698 79543
5698 80537
5698 73975
5698 79604
5699 1348
5699 29926
5699 10084
5699 80351
5700 80093
5700 80092
5700 73392
5700 73579
5700 74767
1200 99999
5698 99999];
data1 = list(:,1)'; % movie id column
data2 = list(:,2)'; % actor id column
idx = [];
col = 1;
while(true)
x = data1(col); % pick the first movie id and count its occurances
for ii = 1:1:size(data1,2)
if(data1(ii) == x)
idx = [idx ii];
end
end
s = sprintf('%u ', data2(idx));
fprintf('actors acted for movie id = %d are: %s\n', x, s);
% delete counted entries
data1(idx) = [];
data2(idx) = [];
idx = [];
if(size(data1,2) <= 0)
break; % break out of while loop if there is no more data to loop through
end
end
actors acted for movie id = 5698 are: 79543 80537 73975 79604 99999 actors acted for movie id = 5699 are: 1348 29926 10084 80351 actors acted for movie id = 5700 are: 80093 80092 73392 73579 74767 actors acted for movie id = 1200 are: 99999

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by