Compare columns of 2 matrices to determine the column values in one matrix that should be paired with the...











up vote
0
down vote

favorite












I have matrix M and N given by



> M
[,1] [,2] [,3] [,4] [,5]
[1,] 5 1 1 7 7
[2,] 4 7 4 2 7
[3,] 11 19 20 50 30

> N
[,1] [,2]
[1,] 7 1
[2,] 7 7


I want to find the column values in M that should be paired with N to get



  [,1] [,2]
7 1
7 7
30 19


I tried the code below. Can i get an efficient way of doing it or especially doing it without using the for commands?



E=numeric()
for (i in 1:2){
for (j in 1:5) {
if (N[1,i]==M[1,j] & N[2,i]==M[2,j]){
E[i]= M[3,j]
}
}
}
E
rbind(N,E)









share|improve this question
























  • what is vect?
    – nate.edwinton
    Nov 19 at 15:18










  • @nate.edwinton I have edited it.
    – user3327637
    Nov 19 at 19:47















up vote
0
down vote

favorite












I have matrix M and N given by



> M
[,1] [,2] [,3] [,4] [,5]
[1,] 5 1 1 7 7
[2,] 4 7 4 2 7
[3,] 11 19 20 50 30

> N
[,1] [,2]
[1,] 7 1
[2,] 7 7


I want to find the column values in M that should be paired with N to get



  [,1] [,2]
7 1
7 7
30 19


I tried the code below. Can i get an efficient way of doing it or especially doing it without using the for commands?



E=numeric()
for (i in 1:2){
for (j in 1:5) {
if (N[1,i]==M[1,j] & N[2,i]==M[2,j]){
E[i]= M[3,j]
}
}
}
E
rbind(N,E)









share|improve this question
























  • what is vect?
    – nate.edwinton
    Nov 19 at 15:18










  • @nate.edwinton I have edited it.
    – user3327637
    Nov 19 at 19:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have matrix M and N given by



> M
[,1] [,2] [,3] [,4] [,5]
[1,] 5 1 1 7 7
[2,] 4 7 4 2 7
[3,] 11 19 20 50 30

> N
[,1] [,2]
[1,] 7 1
[2,] 7 7


I want to find the column values in M that should be paired with N to get



  [,1] [,2]
7 1
7 7
30 19


I tried the code below. Can i get an efficient way of doing it or especially doing it without using the for commands?



E=numeric()
for (i in 1:2){
for (j in 1:5) {
if (N[1,i]==M[1,j] & N[2,i]==M[2,j]){
E[i]= M[3,j]
}
}
}
E
rbind(N,E)









share|improve this question















I have matrix M and N given by



> M
[,1] [,2] [,3] [,4] [,5]
[1,] 5 1 1 7 7
[2,] 4 7 4 2 7
[3,] 11 19 20 50 30

> N
[,1] [,2]
[1,] 7 1
[2,] 7 7


I want to find the column values in M that should be paired with N to get



  [,1] [,2]
7 1
7 7
30 19


I tried the code below. Can i get an efficient way of doing it or especially doing it without using the for commands?



E=numeric()
for (i in 1:2){
for (j in 1:5) {
if (N[1,i]==M[1,j] & N[2,i]==M[2,j]){
E[i]= M[3,j]
}
}
}
E
rbind(N,E)






r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:46

























asked Nov 19 at 15:05









user3327637

356




356












  • what is vect?
    – nate.edwinton
    Nov 19 at 15:18










  • @nate.edwinton I have edited it.
    – user3327637
    Nov 19 at 19:47


















  • what is vect?
    – nate.edwinton
    Nov 19 at 15:18










  • @nate.edwinton I have edited it.
    – user3327637
    Nov 19 at 19:47
















what is vect?
– nate.edwinton
Nov 19 at 15:18




what is vect?
– nate.edwinton
Nov 19 at 15:18












@nate.edwinton I have edited it.
– user3327637
Nov 19 at 19:47




@nate.edwinton I have edited it.
– user3327637
Nov 19 at 19:47












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Here's a way using multiple calls to apply. We iterate over the columns of M and N to find which column in M matches the first column in N and then which matches the second column in N.



logicals <- apply(M[-3,], # exclude third row
2, # iterate over columns
FUN = function(x)
apply(N, 2, #then iterate over columns of N
FUN = function(y) all(x == y)))

# [,1] [,2] [,3] [,4] [,5]
# [1,] FALSE FALSE FALSE FALSE TRUE
# [2,] FALSE TRUE FALSE FALSE FALSE

M[,apply(logicals, 1, which)]

[,1] [,2]
[1,] 7 1
[2,] 7 7
[3,] 30 19


data



M <- structure(c(5, 4, 11, 1, 7, 19, 
1, 4, 20, 7, 2, 50,
7, 7, 30),
.Dim = c(3L, 5L))

N <- structure(c(7, 7, 1, 7), .Dim = c(2L, 2L))





share|improve this answer






























    up vote
    0
    down vote













    Well here is your loop re-written



    E <- vapply(seq(nrow(N)), function(i) M[3,M[1,] == N[1,i] & M[2,] == N[2,i]], numeric(1))
    # with
    > rbind(N,E)
    [,1] [,2]
    7 1
    7 7
    E 30 19


    there is only one loop (vapply - a wrapper for a loop) which runs through the rows of N.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377416%2fcompare-columns-of-2-matrices-to-determine-the-column-values-in-one-matrix-that%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Here's a way using multiple calls to apply. We iterate over the columns of M and N to find which column in M matches the first column in N and then which matches the second column in N.



      logicals <- apply(M[-3,], # exclude third row
      2, # iterate over columns
      FUN = function(x)
      apply(N, 2, #then iterate over columns of N
      FUN = function(y) all(x == y)))

      # [,1] [,2] [,3] [,4] [,5]
      # [1,] FALSE FALSE FALSE FALSE TRUE
      # [2,] FALSE TRUE FALSE FALSE FALSE

      M[,apply(logicals, 1, which)]

      [,1] [,2]
      [1,] 7 1
      [2,] 7 7
      [3,] 30 19


      data



      M <- structure(c(5, 4, 11, 1, 7, 19, 
      1, 4, 20, 7, 2, 50,
      7, 7, 30),
      .Dim = c(3L, 5L))

      N <- structure(c(7, 7, 1, 7), .Dim = c(2L, 2L))





      share|improve this answer



























        up vote
        0
        down vote













        Here's a way using multiple calls to apply. We iterate over the columns of M and N to find which column in M matches the first column in N and then which matches the second column in N.



        logicals <- apply(M[-3,], # exclude third row
        2, # iterate over columns
        FUN = function(x)
        apply(N, 2, #then iterate over columns of N
        FUN = function(y) all(x == y)))

        # [,1] [,2] [,3] [,4] [,5]
        # [1,] FALSE FALSE FALSE FALSE TRUE
        # [2,] FALSE TRUE FALSE FALSE FALSE

        M[,apply(logicals, 1, which)]

        [,1] [,2]
        [1,] 7 1
        [2,] 7 7
        [3,] 30 19


        data



        M <- structure(c(5, 4, 11, 1, 7, 19, 
        1, 4, 20, 7, 2, 50,
        7, 7, 30),
        .Dim = c(3L, 5L))

        N <- structure(c(7, 7, 1, 7), .Dim = c(2L, 2L))





        share|improve this answer

























          up vote
          0
          down vote










          up vote
          0
          down vote









          Here's a way using multiple calls to apply. We iterate over the columns of M and N to find which column in M matches the first column in N and then which matches the second column in N.



          logicals <- apply(M[-3,], # exclude third row
          2, # iterate over columns
          FUN = function(x)
          apply(N, 2, #then iterate over columns of N
          FUN = function(y) all(x == y)))

          # [,1] [,2] [,3] [,4] [,5]
          # [1,] FALSE FALSE FALSE FALSE TRUE
          # [2,] FALSE TRUE FALSE FALSE FALSE

          M[,apply(logicals, 1, which)]

          [,1] [,2]
          [1,] 7 1
          [2,] 7 7
          [3,] 30 19


          data



          M <- structure(c(5, 4, 11, 1, 7, 19, 
          1, 4, 20, 7, 2, 50,
          7, 7, 30),
          .Dim = c(3L, 5L))

          N <- structure(c(7, 7, 1, 7), .Dim = c(2L, 2L))





          share|improve this answer














          Here's a way using multiple calls to apply. We iterate over the columns of M and N to find which column in M matches the first column in N and then which matches the second column in N.



          logicals <- apply(M[-3,], # exclude third row
          2, # iterate over columns
          FUN = function(x)
          apply(N, 2, #then iterate over columns of N
          FUN = function(y) all(x == y)))

          # [,1] [,2] [,3] [,4] [,5]
          # [1,] FALSE FALSE FALSE FALSE TRUE
          # [2,] FALSE TRUE FALSE FALSE FALSE

          M[,apply(logicals, 1, which)]

          [,1] [,2]
          [1,] 7 1
          [2,] 7 7
          [3,] 30 19


          data



          M <- structure(c(5, 4, 11, 1, 7, 19, 
          1, 4, 20, 7, 2, 50,
          7, 7, 30),
          .Dim = c(3L, 5L))

          N <- structure(c(7, 7, 1, 7), .Dim = c(2L, 2L))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 20:19

























          answered Nov 19 at 20:07









          bouncyball

          6,331523




          6,331523
























              up vote
              0
              down vote













              Well here is your loop re-written



              E <- vapply(seq(nrow(N)), function(i) M[3,M[1,] == N[1,i] & M[2,] == N[2,i]], numeric(1))
              # with
              > rbind(N,E)
              [,1] [,2]
              7 1
              7 7
              E 30 19


              there is only one loop (vapply - a wrapper for a loop) which runs through the rows of N.






              share|improve this answer



























                up vote
                0
                down vote













                Well here is your loop re-written



                E <- vapply(seq(nrow(N)), function(i) M[3,M[1,] == N[1,i] & M[2,] == N[2,i]], numeric(1))
                # with
                > rbind(N,E)
                [,1] [,2]
                7 1
                7 7
                E 30 19


                there is only one loop (vapply - a wrapper for a loop) which runs through the rows of N.






                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Well here is your loop re-written



                  E <- vapply(seq(nrow(N)), function(i) M[3,M[1,] == N[1,i] & M[2,] == N[2,i]], numeric(1))
                  # with
                  > rbind(N,E)
                  [,1] [,2]
                  7 1
                  7 7
                  E 30 19


                  there is only one loop (vapply - a wrapper for a loop) which runs through the rows of N.






                  share|improve this answer














                  Well here is your loop re-written



                  E <- vapply(seq(nrow(N)), function(i) M[3,M[1,] == N[1,i] & M[2,] == N[2,i]], numeric(1))
                  # with
                  > rbind(N,E)
                  [,1] [,2]
                  7 1
                  7 7
                  E 30 19


                  there is only one loop (vapply - a wrapper for a loop) which runs through the rows of N.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 9:18

























                  answered Nov 19 at 15:20









                  nate.edwinton

                  901314




                  901314






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377416%2fcompare-columns-of-2-matrices-to-determine-the-column-values-in-one-matrix-that%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'