how to find elements that meet multiple conditions in cell array

6 views (last 30 days)

Hello, i am new to matlab and i have to solve some problems for my university exams. I have given a file certificates.list that contains a lot of movies, the year shown, the countries and the film rating( not all films have ratings). The file is a cell type class with 374045 rows and one column. The rows look like this

Ôritsu uchûgun Oneamisu no tsubasa (1987) South Korea:15

Ôritsu uchûgun Oneamisu no tsubasa (1987) UK:15

Ôritsu uchûgun Oneamisu no tsubasa (1987) USA:Unrated

Êtes-vous fiancée à un marin grec ou à un pilote de ligne? (1971) Finland:S

Êtes-vous fiancée à un marin grec ou à un pilote de ligne? (1971) West Germany:16

Être et avoir (2002) Taiwan:GP

Être et avoir (2002) Iceland:L

Être et avoir (2002) Singapore:PG

i have questions like "what is the film rating for movie A in country B" ( exact match with 2 conditions) or "is the film rating for movie A in countries B and C?"( exact match with 3 conditions).

Can anyone give me a hint on how o answer these questions? As i am new at matlab i cant think anything that can help me.

Thanks in advance

  5 Comments
jim ...
jim ... on 16 Mar 2016
Edited: jim ... on 16 Mar 2016
We are free to use anything. The exercise does note state any limitations at all. As it is a university course they told us that they dont want to solve the exercise based only at what the taught us in class( almost nothing concerning the solution of this exercise). We are free to use anything as long as it works.
Image Analyst
Image Analyst on 16 Mar 2016
What a refreshing change from most homework assignments that says you can't use the full power of MATLAB.

Sign in to comment.

Accepted Answer

John BG
John BG on 16 Mar 2016
Hi Jim
in order to answer the kind of questions you are facing you need a structure you can easily select elements according to questions. One way of building such structure is with the class table
file_name='ratings1.txt' % open file
fileID=fopen(file_name)
str1={} % acquire lines in cell named str1
tline=fgetl(fileID)
str1=[str1 ; tline]
while ischar(tline)
tline = fgetl(fileID);
str1=[str1 ; tline]
end
fclose(fileID);
[sz1 sz2]=size(str1)
since from the sample supplied there only one '(', one ')' and one ':' in each line let's assume that no film name will have any of these characters:
film_name={};year={};country={};rating={};
for k=1:sz1-1
L1=str1{k}
split1=regexp(L1,'(')
split2=regexp(L1,')')
split3=regexp(L1,':')
L1_name=L1([1:split1-1])
L1_year=L1([split1+1:split2-1])
L1_country=L1([split2+2:split3-1])
L1_rating=L1([split3+1:end])
film_name=[film_name;L1_name]
year=[year;L1_year]
country=[country;L1_country]
rating=[rating;L1_rating]
end
T=table(film_name,year,country,rating)
Now you how to answer questions like the ones you have mentioned in the question:
because sure the film name length vary, you need strcmp() or isequal()
One way is to build yourself functions to search the structure, like
function pointer=cell_comp(cell1,name1)
% search for string name1 in cell1, cell1 being Nx1 strings
% cell{1}='string1' cell{2}='string2_var_length' ..
% return position where name1 may be
pointer=0
[s1 s2]=size(cell1)
for k=1:s1
if isequal(cell1{k},name1)
pointer=[pointer k]
end
end
if sum(pointer)>0
pointer(1)=[]
end
end
to the question "what is the film rating for movie A in country B"
A='Ôritsu uchûgun Oneamisu no tsubasa'
film_A_entries=cell_comp(T.film_name,A)
B='Iceland'
country_B_entries=cell_comp(T.country,B)
intersect(film_A_entries,country_B_entries)
ans = Empty matrix: 1-by-0
there is no no such film A in country B
for
A='Êtes-vous fiancée à un marin grec ou à un pilote de ligne?'
B='Finland'
film_A_entries=cell_comp(T.film_name,A)
country_B_entries=cell_comp(T.country,B)
the answer now is
T.rating(intersect(film_A_entries,country_B_entries))
=
'S'
To the question: what is the film rating for movie A in countries B and C
A='Être et avoir'
B='Iceland'
C='Taiwan'
film_A_entries=cell_comp(T.film_name,A)
country_B_entries=cell_comp(T.country,B)
country_C_entries=cell_comp(T.country,C)
T.rating([country_B_entries country_c_entries]) =
'L'
'GP'
but in for this question
A='Ôritsu uchûgun Oneamisu no tsubasa'
B='USA'
C='Singapore'
film_A_entries=cell_comp(T.film_name,A)
country_B_entries=cell_comp(T.country,B)
country_C_entries=cell_comp(T.country,C)
T.rating([country_B_entries country_C_entries])=
'Unrated'
'PG'
wrong because film A is not listed in country C
The basic answer would be
intersect(intersect(film_A_entries,country_B_entries),country_C_entries)=
Empty matrix: 1-by-0
because the condition queried in the question does not exist.
If you want elaborate you could do instead the following:
T.rating(intersect(film_A_entries,country_B_entries)) =
'Unrated'
T.rating(intersect(film_A_entries,country_C_entries)) =
Empty cell array: 0-by-1
Please understand this is just one basic way to address the main line of your question.
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John

More Answers (1)

Image Analyst
Image Analyst on 16 Mar 2016
Hint: try functions such as cellfun(), cell2table(), strfind(), or ismember(), etc.

Categories

Find more on Programming 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!