Using forloop and whileloop to find if a number has appeared repetitively
Show older comments
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
Accepted Answer
More Answers (1)
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
Categories
Find more on Actors in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!