Return variables or elements with similar corresponding values

Hi all,
I have an excel sheet with two columns. It's basically a list of big cities in the world and the population of each one of them. I want to extract the cities that have the same population is there any MATLAB function can do that? I searched and can't find the right function. I would really appreciate any help. The file is attached.
Thanks in advance

 채택된 답변

Adam Danz
Adam Danz 2021년 4월 13일

0 개 추천

I'm currious what you searched for. Are you looking for a Matlab function that reads in an excel file that has two columns of data and returns the values in column 1 that have matching values in column 2? That would be a really specific function.
Here's the steps you need to take. If you get stuck, show us what you've got and we can help you get unstuck.
  1. Read in the data (see readtable)
  2. Use groupcounts to find exact duplicate population sizes
  3. Use indexing to return the city names that have matches

댓글 수: 4

I appreciate the reply! I know how to read an excel file in matlab but I was looking for function like setdiff, ismember, intersect etc. to extract the cities have the same exact population. For instance
Ekurhuleni and Casablanca have the same population. I don’t think your method of groupcounts would work for this particular case.
> I don’t think your method of groupcounts would work for this particular case.
Of course it would. In fact, this solution only requires 4 lines of code.
Here's a proof of concept but I've removed key components. Let us know if you have any trouble filling them in.
% Read in table
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/582081/File.xlsx');
% Add column that identifies duplicate rows
[duplicateCount, duplicatePop] = groupcounts(T.Population);
T.HasDuplicate = ismember(T.Population, duplicatePop(duplicateCount>1))
T = 86×3 table
City Population HasDuplicate _____________ __________ ____________ {'Shanghai' } 1.7836e+07 false {'Lagos' } 1.492e+07 false {'Istanbul' } 1.416e+07 false {'Karachi' } 1.3125e+07 false {'Mumbai' } 1.2478e+07 false {'Moscow' } 1.2111e+07 false {'São Paulo'} 1.1822e+07 false {'Beijing' } 1.1717e+07 false {'Guangzhou'} 1.1071e+07 false {'Delhi' } 1.1008e+07 false {'Lahore' } 1.052e+07 false {'Shenzhen' } 1.0467e+07 false {'Seoul' } 1.0442e+07 false {'Jakarta' } 9.7614e+06 false {'Tianjin' } 9.3418e+06 true {'Chennai' } 8.9811e+06 false
% Return rows that have duplicates
T(T.HasDuplicate,:)
ans = 11×3 table
City Population HasDuplicate ____________________ __________ ____________ {'Tianjin' } 9.3418e+06 true {'Dhaka' } 8.906e+06 true {'Ho Chi Minh City'} 8.906e+06 true {'Rio de Janeiro' } 8.906e+06 true {'Riyadh' } 8.906e+06 true {'Casablanca' } 9.3418e+06 true {'Berlin' } 3.4011e+06 true {'Hefei' } 3.4011e+06 true {'Ekurhuleni' } 9.3418e+06 true {'Changsha' } 9.3418e+06 true {'Wenzhou' } 9.3418e+06 true
Ah I see! I completely misunderstood your initial steps when you mentioned indexing. Thank you and I apologize I didn't mean any disrespect or challenge your knowledge like some people do, when I said it doesn’t work. I had it completely wrong in my head! I appreciate the help!
No disrespect take. "It didn't work" are among the 3 most common words in many replies in this forum but they usually mean I didn't understand or I couldn't implement the solution 😁.
I assume you figured out the rest of the solution so I'll update my previous comment to fill in the blanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Fletcher
David Fletcher 2021년 4월 13일
편집: David Fletcher 2021년 4월 13일
Essentially you would subtract a square matrix formed from the list of populations copied to form a square matrix and subtract the transpose of the same matrix to give the population of every city subtracted from every other cities' population. You can then search that matrix for zero values (if you want exact population matches), or visualize it.
populations=File{:,2};
popMatrix=repmat(populations,1,86); %Extract population list and copy to form square matrix
comparison=popMatrix-popMatrix'; %subtract transposed matrix to give population of every city subtracted from every other city
comparison=abs(comparison)
heatmap(comparison,'ColorMethod','none','ColorScaling','log') %visualize data

댓글 수: 3

Simpler to use implicit expansion:
populations=File{:,2};
comparison = abs(populations(:) - populations(:).');
Of course if you're looking for 0's the abs() can be skipped.
David Fletcher
David Fletcher 2021년 4월 14일
편집: David Fletcher 2021년 4월 14일
Just relearning Matlab after a long absence, so I'm a bit rusty, though I'm not sure I was aware of the implicit expansion trick the last time I had to use this package, so thanks for that. The abs was originally because I thought it might better facilitate a tolerance search for values close to zero rather than rely on having two populations to be exactly the same, but the OP seemed definite that he wanted exact matches so I didn't bother mentioning it, but left the line in. Anyway, thanks for your input.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 4월 13일

댓글:

2021년 4월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by