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

  • Home
  • SEARCH
  • 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 6163957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:54:44+00:00 2026-05-23T21:54:44+00:00

I am facing a problem in matching elements in 2 matrices. The first element

  • 0

I am facing a problem in matching elements in 2 matrices. The first element can be matched using ismember but the second element should be within a range. Please see the example below:

% col1 is integerID, col2 is a number.       -->col1 is Countrycode, col2 is date
bigmat = [ 600 2  
           600 4  
           800 1  
           800 5  
           900 1] ;

% col1 is integerID, col2 is VALUE, col2 is a range    -->value is Exchange rate
datamat = {...
           50   0.1   [2 3 4 5 6]       % 2:6
           50   0.2   [9 10 11]         % 9:11
           600  0.01  [1 2 3 4]         % 1:4
           600  0.2   [8 9 10]          % 8:10
           800  0.12  [1]               % 1:1
           800  0.13  [3 4]             % 3:4
           900  0.15  [1 2]      } ;    % 1:2

I need the answer as:
    ansmat = [ 600 2  0.01
               600 4  0.01
               800 1  0.12
               800 5  nan               % even deleting this row is fine
               930 1  0.15 ] ;

For simplicity:

  1. All intIDs from matrix_1 exist in matrix_2.
  2. The numbers in range are dates! Within a range, these numbers are consecutive: [1 2…5]
  3. For any ID, dates in the next row are not continuous. Eg, you can see [1 2 3 4] and then [8 9 10] in next row.

bigmat is a huge matrix! 300,000-500,000 rows and so a vectorized code would be appreciated. datamat is roughly 5000 rows or less. You can convert the cell to matrix. For each row, I have the minimum and maximum. The 3 column is minimum:maximum. Thanks!

  • 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-23T21:54:44+00:00Added an answer on May 23, 2026 at 9:54 pm

    Here is one possible implementation:

    %# data matrices
    q = [
        600 2  
        600 4  
        800 1  
        800 5  
        900 1
    ];
    M = {
        50   0.1   [2 3 4 5 6]
        50   0.2   [9 10 11]
        600  0.01  [1 2 3 4]
        600  0.2   [8 9 10]
        800  0.12  [1]
        800  0.13  [3 4]
        900  0.15  [1 2]
    };
    
    %# build matrix: ID,value,minDate,maxDate
    M = [cell2num(M(:,1:2)) cellfun(@min,M(:,3)) cellfun(@max,M(:,3))];
    
    %# preallocate result
    R = zeros(size(M,1),3);
    
    %# find matching rows
    c = 1;             %# counter
    for i=1:size(q,1)
        %# rows indices matching ID
        ind = find( ismember(M(:,1),q(i,:)) );
    
        %# out of those, keep only those where date number is in range
        ind = ind( M(ind,3) <= q(i,2) & q(i,2) <= M(ind,4) );
    
        %# check if any
        num = numel(ind);
        if num==0, continue, end
    
        %# extract matching rows
        R(c:c+num-1,:) = [M(ind,1) repmat(q(i,2),[num 1]) M(ind,2)];
        c = c + num;
    end
    
    %# remove excess
    R(c:end,:) = [];
    

    The result as expected:

    >> R
    R =
              600            2         0.01
              600            4         0.01
              800            1         0.12
              900            1         0.15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing this problem again and again, but can't find any solution (except mine,
I am trying to hide an array of elements but facing problem while writing
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
Im facing problem with poor performance of css prompting mechanism. Can I dissable this
I creates program in c/c++ in turbo c . but i am facing problem
I am using Eclipse Helios. Last days I am facing problem that Eclipse Helios
Facing problem with every fetch i want to make using EF Query model.Place here
I am facing a very basic problem. Some time small things can take your
I am facing problem for reading xml file using javascript. It works fine in
I am facing problem while calling the alert box. Can I know what is

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.