How to select corresponding rows of a table based on selected rows of one column?

17 views (last 30 days)
Hi
I have a table in matlab containing 3379 rows and 4 columns. The below comes the first 8 rows of in question table:
as it can be seen the numbers in pagenumber columns repeated themselves (different time like 2, 3 and so on), i want to have numbers in pagenumber column without repeating (select the same number that have max area for instance for 1765 select the row 423 0/49302 6 1765. I have used the cod below but i can just select max area column and i dont know how it should be changed that all other rows correspond to the max area be selected too. ( my goal is have the rows with max area and corresponding Geo_cod and PageNumber to that area).
FID Area1 Geo_cod PageNumber
423 0/49302 6 1765
660 0/476409 0 1765
2696 0/03057 18 1765
219 0/939628 6 1766
1026 0/060377 0 1766
1854 0/325402 0 1768
2401 0/6746 6 1768
1132 0/873215 0 1769
I used the below cod:
[c,ia,ib] = unique(t1(:,4)) ;
C = zeros(length(ia),1) ;
for i = 1:length(ia)
C(i) = max(t1.Area(ib==i));
End
It will give me just max area like below and i dont know what geo codes these areas are belong to?
0.963917000000000
0.930372000000000
0.974875000000000
0.980273000000000
0.974578000000000
I am novice user in matlab and any help will highly appreciated.

Accepted Answer

Jon
Jon on 17 Nov 2021
Edited: Jon on 17 Nov 2021
T = readtable('sadegh.geo_cod.xlsx');
% sort the rows of table in descending order of areas
Tsrt = sortrows(T,'Area1','descend');
% find unique page numbers
% index lists first occurence of duplicate variables, but since table is sorted
% by area this will be the one with the largest area
[~,ia] = unique(Tsrt.PageNumber);
% make new table with duplicates removed only retaining rows with largest
% areas
Tnew = Tsrt(ia,:);
% sort back to have table with increasing page numbers
Tnew = sortrows(Tnew,'PageNumber');
  3 Comments
Jon
Jon on 18 Nov 2021
That's great! If that answered your question, if you haven't already, please accept the answer so others will know an answer is available - thanks

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!