Using forloop and whileloop to find if a number has appeared repetitively
조회 수: 2 (최근 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
댓글 수: 0
채택된 답변
Dyuman Joshi
2021년 11월 30일
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
댓글 수: 4
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
for i=1:numel(y)
z2{i} = [y(i) data(find(data(:,1)==y(i)),2)'];
end
z2
추가 답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Aerospace Blockset에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!