Return variables or elements with similar corresponding values

3 ビュー (過去 30 日間)
LeoAiE
LeoAiE 2021 年 4 月 13 日
コメント済み: Adam Danz 2021 年 4 月 14 日
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 日
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 件のコメント
LeoAiE
LeoAiE 2021 年 4 月 14 日
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!
Adam Danz
Adam Danz 2021 年 4 月 14 日
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 件のコメント
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.
Adam Danz
Adam Danz 2021 年 4 月 14 日
Implicit expansion came out in r2016b.
And welcome back! :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by