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
채택된 답변
추가 답변 (1개)
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
Adam Danz
2021년 4월 14일
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
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
2021년 4월 14일
Implicit expansion came out in r2016b.
And welcome back! :)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!