Apologies if a similar question has already been asked.
I am trying to compare two lists of company names and have created a sequence of if statements within a for loop, with a contains() function in the if statements.
The logic is that for each company name X in the original list, the if statements will be computed:
if: If there is a match in the top 50 of both lists, then I get the name of the company with the statement "X is in the 2020 top 50 list".elseif: If there is a match in the top 50 of the original list and the rest of the secondary list then I get the name of the company with the statement "X is in the 2020 overall list"else: If there is no match, I get the name of the company that did not match with the statement "X is not in the 2020 list"
As of now my output is all the companies that are matches but with statement in (3) i.e. my else statement. Not sure why, but my else statement is capturing my if and elseif statement output.
Code:
year1 = data(1:end, 1:4); %tables
year30 = data(1:end, 121:124);
names30_50 = year30{1:50, 2}; %cells
names30_all = year30{:, 2};
names1_50 = year1{1:50, 2};
names1_all = year1{:, 2};
for i = 1:height(names1_50)
if contains(names30_50, names1_50{i}) %comparing both top 50 lists
disp(names1_50{i})
fprintf('%s is in the 2020 top 50 list.\n',names1_50{i})
elseif contains(names30_all, names1_50{i}) %comparing top 50 of 1990 with all of 2020
disp(names1_50{i})
fprintf('%s is in the 2020 overall list.\n',names1_50{i})
else
fprintf('%s is not in the 2020 list.\n',names1_50{i});
end
end
Output example:
General Motors is not in the 2020 list.
Ford Motor is not in the 2020 list.
Exxon Mobil is not in the 2020 list.
Intl. Business Machines is not in the 2020 list.
General Electric is not in the 2020 list.
Mobil is not in the 2020 list.
Altria Group is not in the 2020 list.
Chrysler is not in the 2020 list.
It goes on, but all of these companies are in both lists.
Aucun commentaire:
Enregistrer un commentaire