简体   繁体   中英

How to select only certain rows in a matrix in Matlab?

I have a matrix A in Matlab

A= [1 2 3 |1;
    2 3 4 |2; 
    5 6 7 |2; 
    3 4 5 |1; 
    6 7 0 |3; 
    6 3 7 |3; 
    4 5 3 |1; 
    6 5 4 |4]

where the last column contains natural indices possibly repeated. For each index in the last column, I want to select the first row of A associated with that index and create the matrix

B=[1 2 3 |1;
   2 3 4 |2;
   6 7 0 |3;
   6 5 4 |4]

Use unique to get the values and indices you require:

[U,I] = unique(A(:,4), 'first')

Then

A(I,:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM