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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:08:54+00:00 2026-06-15T13:08:54+00:00

This is a fairly simple task I want to perform, but I can’t seem

  • 0

This is a fairly simple task I want to perform, but I can’t seem to figure out a way to do it. I’ve tried sortrows, reshaping, and other solutions, but none of them do exactly what I want.

Essentially, I have two vectors from the same range of values, of unequal lengths. Some of the values are equal, some are not. E.g.

A = [1 5 20 30 53 70 92]
B = [2 3 4 16 20 30 60 95 100]

What I want to do is add “NaNs” to each vector to “stand in” for the values in the other vector that aren’t shared. So, I want them to look like:

A = [1 NaN NaN NaN 5 NaN 20 30 53 NaN 70 92 NaN NaN]
B = [NaN 2 3 4 NaN 16 20 30 NaN 60 NaN NaN 95 100]

Some method by which the vector will have placeholders for the value of the other vector.

Do I combine the vectors, sort it, then somehow search and replace all values from the other vector with NaNs? That seems like a bit of a clunky solution, though not impossible. I feel like there is some more elegant way to accomplish this that I am missing.

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-06-15T13:08:55+00:00Added an answer on June 15, 2026 at 1:08 pm

    Here is one solution using a simple map:

    A = [1 5 20 30 53 70 92]
    B = [2 3 4 16 20 30 60 95 100]
    
    % map all A and B elements
    % use 1 for A and 2 for B
    map = zeros(max([A,B]),1);
    map(A) = 1;
    map(B) = bitor(map(B), 2);
    
    % find the values present in either A, or B
    [~,~,j] = find(map);
    AA = nan(size(j));
    BB = nan(size(j));
    AA(bitand(j,1)~=0) = A;
    BB(bitand(j,2)~=0) = B;
    

    Comparison with Rodys solution shows this method is a bit faster:

    A = unique(randi(10000, 1000, 1));
    B = unique(randi(10000, 1000, 1));
    
    tic;
    for i=1:1000
        map=zeros(10000,1);
        map(A) = 1;
        map(B) = bitor(map(B), 2);
        [~,~,j] = find(map);
        AA = nan(size(j));
        BB = nan(size(j));
        AA(bitand(j,1)~=0) = A;
        BB(bitand(j,2)~=0) = B;
    end
    toc
    
    
    tic
    for i=1:1000
        C = union(A,B);
        Ap = NaN(size(C));   
        Ap(ismember(C,A)) = A;
        Bp = NaN(size(C));   
        Bp(ismember(C,B)) = B;
    end
    toc
    
    isequalwithequalnans(BB, Bp)
    isequalwithequalnans(AA, Ap)
    
    Elapsed time is 0.283828 seconds.
    Elapsed time is 0.457204 seconds.
    
    ans =
     1
    
    ans =
     1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is fairly simple but I cant work it out. I'm building a wordpress
I thought this would be fairly simple but it turns out not to work
I want to automate a fairly simple task. For this I have written a
The task seems fairly simple but I don't know what's the best way to
This is fairly simple in Windows, but a little tricky in Linux. I am
I guess this is fairly simple for you but i cant wrap my head
I believe this is a fairly simple question but it is something I am
This seems like another fairly simple thing to do, but I'm again struggling on
This should be a fairly simple question to answer, but I am interested to
This seems like it would be fairly simple, but I've been unable to find

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.