2

I have two giant array which looks like:

A = [11, 11, 12, 3, 3, 4, 4, 4 ];
B = [ 12, 4; 3, 11; 11, 1; 4, 13 ];

I want to create an array which takes values from B and column 1 from A to look like:

C = [ 11, 1; 11, 1; 12, 4; 3, 11; 3, 11; 4, 13; 4, 13; 4, 13 ];

I don't want to use for or any other kind of loop to optimize the process.

Sorry for being terse.

I will search each element from column 1 of A in column 1 of B and pick the corresponding column 2 elements from B and create a new array with column 1 elements of A and discovered column 2 elements from B.

2
  • 2
    this needs EVEN MORE explaining! Commented Aug 19, 2015 at 16:01
  • Done. Check my edit. Commented Aug 19, 2015 at 16:15

2 Answers 2

3

What you are doing in this problem is using A and searching the first column of B to see if there's a match. Once there's a match, extract out the row that corresponds to this matched location in B. Repeat this for the rest of the values in A.

Assuming that all values of A can be found in B and that the first column of B is distinct and that there are no duplicates, you can a unique call and sortrows call. The unique call is on A so that you can assign each value in A to be a unique label in sorted order. You would then use these labels to index into the sorted version of B to get your desired result:

[~,~,id] = unique(A);
Bs = sortrows(B);
C = Bs(id,:);

We get for C:

C =

    11     1
    11     1
    12     4
     3    11
     3    11
     4    13
     4    13
     4    13
Sign up to request clarification or add additional context in comments.

4 Comments

Your answer doesn't require an intermediate variable. On the other hand, it needs to sort a larger matrix
@LuisMendo - Correct! I suspect that your answer will be more efficient in the long run.
Sorry the answer was great but my problem was not well formulated I suppose. I have modified my problem statement.
@user1243255 you're very welcome. Sorry for being terse with you earlier... just that you new edit didn't make any sense until you corrected yourself.
3

Thanks to @rayryeng for clarifying the question to me.

  • Assuming each element from A is present in column 1 of B:

    [~, ind] = max(bsxfun(@eq, A(:).', B(:,1)), [], 1);
    C = B(ind,:);
    
  • If that assumption doesn't necessarily hold:

    [val, ind] = max(bsxfun(@eq, A(:).', B(:,1)), [], 1);
    C = B(ind(val),:);
    

    So for example A = [11, 20, 12, 3, 3, 4, 4, 4 ]; would produce

    C =
        11     1
        12     4
         3    11
         3    11
         4    13
         4    13
         4    13
    

1 Comment

Sorry the answer was great but my problem was not well formulated I suppose. I have modified my problem statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.