Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:57:43+00:00 2026-05-26T13:57:43+00:00

I have a matrix which has multiple rows per ID. I need to extract

  • 0

I have a matrix which has multiple rows per ID. I need to extract only say..4 LAST rows per ID. ID cols consists of 2 Cols:

mat = [ ...
        2222 1   734771 0.11
        2222 1   734772 0.22
        2222 1   734773 0.33
        2222 1   734774 0.44
        2222 1   734775 0.55
        5555 3   734771 0.11
        5555 1   734772 0.12
        5555 1   734773 0.13
      ] ;

answer –>
% [ 2222 1 734772 0.22 ; 2222 1 734773 0.33 ; 2222 1 734774 0.44 ; 2222 1 734775 0.55]

I’ll appreciate a vectorized approach.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:57:43+00:00Added an answer on May 26, 2026 at 1:57 pm

    Here is a efficient solution using UNIQUE and ACCUMARRAY functions:

    %# sample matrix, sorted by first column
    mat = [randi([1 5],[20 1]) (1:20)' rand(20,1)];   %'
    mat = sortrows(mat);
    
    %# extract last N rows from each unique ID
    N = 4;
    [~,~,subs] = unique( mat(:,1) );                 %# index into unique values
    fcn = @(x){ x(max(1,end-N+1):end) };             %# at most last N values
    ind = accumarray(subs, 1:numel(subs), [max(subs) 1], fcn);
    ind = cell2mat( ind(cellfun(@numel,ind) >= 4) ); %# keep only those with 4+ count
    result = mat(ind,:)
    

    Using the third output of the UNIQUE function, we get for each row the index into the list of unique IDs. Then we distribute the row indices into bins (cell array) based on those subscripts. We take the last four indices from each bin, and filter those with less than 4 occurrences. We then combine them all into one vector of row indices. Finally we use it to get the corresponding rows from the original matrix.

    Using the example above, we generate the following matrix:

    mat =
                1            2      0.70199
                1            6      0.46313
                1            7      0.98821
                1           12      0.15645
                1           13      0.67037
                1           16      0.86966
                2            8      0.63491
                2            9     0.076501
                2           15      0.55076
                2           17      0.44727
                2           19      0.30587
                3            5      0.91502
                3           10      0.97322
                3           20      0.48231
                4            3      0.45633
                4            4      0.12363
                4           11      0.18319
                4           14      0.36045
                5            1      0.36708
                5           18      0.63084
    

    and the result was:

    result =
                1            7      0.98821
                1           12      0.15645
                1           13      0.67037
                1           16      0.86966
                2            9     0.076501
                2           15      0.55076
                2           17      0.44727
                2           19      0.30587
                4            3      0.45633
                4            4      0.12363
                4           11      0.18319
                4           14      0.36045
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a matrix x which contains 10 rows and 2 columns.
I have a data set which consists of an ID and a matrix (n
I have a 30000x14000 sparse matrix in MATLAB (version 7), which I need to
Suppose I have a integer matrix which represents who has emailed whom and how
i have an application which has 3X4 matrix of images using arrow key(up,right,top,bottom) i
I have a matrix that has 20 rows and 51 columns. I would like
I have a 2D matrix mat with 500 rows × 335 columns, and a
I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns
I have a 4x4 matrix which I wish to decompose into 4 frequency bands
I have a code base, in which for Matrix class, these two definitions are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.