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 6682647
In Process

The Archive Base Latest Questions

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

I have an interesting problem that involves taking the last day from a matrix

  • 0

I have an interesting problem that involves taking the last day from a matrix and finding its last month day. Eg, if the date today is Oct-10-2011, you try to search for Sep-10-2011 or the first day < Sep-10-2011 in the matrix.

Matrix has multiple IDs and last trading dates may not be the same.
Vectorized solution is desired. Thanks!

mat = [
 1000 734507 11 ; 1000 734508 12 ; 1000 734509 13 ; 
 2001 734507 21 ; 2001 734508 22 ; 2001 734513 23 ; 2001 734516 25 ;
 1000 734536 14 ; 1000 734537 15 ; 1000 734538 16 ; 
 2001 734536 26 ; 2001 734537 27 ; 2001 734544 28 ; 2001 734545 29;2001 734546 30
];

% datestr(mat(:,2))
[~,m,~] = unique(mat(:,1), 'rows', 'last') ;
lastDay = mat(m,;) ;

Tried using addtodate to get last-month-date here but it fails (more than 1 row)

Once I get the last-dates for each ID, I need to get the exact_day_lastmonth. After this, I need to get data on this day OR the day nearest to it (should be < exact_day_lastmonth).

Answer:

current_lastdays = [1000 734538 16 ;  2001 734546 30] ; % 4-Feb-2011, 12-Feb-2011
matching_lastmon = [1000 734507 11 ;  2001 734513 23] ; % 4-Jan-2011, 10-Jan-2011
  • 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-26T04:43:51+00:00Added an answer on May 26, 2026 at 4:43 am

    Unless you want to risk rather large arrays with complicated indexing, I think a loop is the way to go.

    mat = [ 1000 734507 11 ; 1000 734508 12 ; 1000 734509 13 ; 
            2001 734507 21 ; 2001 734508 22 ; 2001 734513 23 ; 2001 734516 25 ;
            1000 734536 14 ; 1000 734537 15 ; 1000 734538 16 ; 
    2001 734536 26 ; 2001 734537 27 ; 2001 734544 28 ;2001 734545 29;2001 734546 30];
    
    %# datestr(mat(:,2))
    [~,m,~] = unique(mat(:,1), 'rows', 'last') ;
    lastDay = mat(m,;) ;
    
    matching_lastmon = lastDay; %# initialize matching_lastmon
    oneMonthBefore = datenum(bsxfun(@minus,datevec(lastDay(:,2)),[0,1,0,0,0,0]));
    
    for iDay = 1:size(lastDay,1)
        %# the following assumes that the array `mat` is sorted within each ID (or globally sorted by date)
    
        idx = find(mat(:,1)==lastDay(iDay,1) & mat(:,2) <= oneMothBefore(iDay),1,'last')
    
        if isempty(idx)
            matching_lastmon(iDay,2:3) = NaN;
        else
            matching_lastmon(iDay,:) = mat(idx,:);
        end
    
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting problem. The basis of the problem is that my last
I have an interesting SQL problem that I need help with. Here is the
I have an interesting genetics problem that I would like to solve in native
He're an interesting problem that looks for the most Pythonic solution. Suppose I have
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
We have an interesting problem that I was hoping someone could help to shed
I have an interesting problem, that i am sure has a simple answer, but
Here's an interesting problem that I have just come across. It is possible to
I have an interesting problem.. I have a Form that launches another Form (2)
I have an interesting problem. I currently have a basic template library that renders

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.