F# Math.Net Matrix.mapRows to create new matrix with different size












1















I have a function that manipulates a Vector<float> resulting a new Vector<float> with different length, an example would be appending a number in front of the vector



let addElementInfront (x:Vector<float>) =
x.ToArray()
|> Array.append [|x.[0]|]
|> vector


Now I want to apply this to all the rows of a (2x2) matrix and I would expect a (2x3) matrix, I tried to use the Matrix.mapRows of MathNet.Numerics.LinearAlgebra but it gives me error that the size needs to be the same.



Just wonder if MathNet has any other function to map rows that results a different size matrix.



Thanks.










share|improve this question



























    1















    I have a function that manipulates a Vector<float> resulting a new Vector<float> with different length, an example would be appending a number in front of the vector



    let addElementInfront (x:Vector<float>) =
    x.ToArray()
    |> Array.append [|x.[0]|]
    |> vector


    Now I want to apply this to all the rows of a (2x2) matrix and I would expect a (2x3) matrix, I tried to use the Matrix.mapRows of MathNet.Numerics.LinearAlgebra but it gives me error that the size needs to be the same.



    Just wonder if MathNet has any other function to map rows that results a different size matrix.



    Thanks.










    share|improve this question

























      1












      1








      1








      I have a function that manipulates a Vector<float> resulting a new Vector<float> with different length, an example would be appending a number in front of the vector



      let addElementInfront (x:Vector<float>) =
      x.ToArray()
      |> Array.append [|x.[0]|]
      |> vector


      Now I want to apply this to all the rows of a (2x2) matrix and I would expect a (2x3) matrix, I tried to use the Matrix.mapRows of MathNet.Numerics.LinearAlgebra but it gives me error that the size needs to be the same.



      Just wonder if MathNet has any other function to map rows that results a different size matrix.



      Thanks.










      share|improve this question














      I have a function that manipulates a Vector<float> resulting a new Vector<float> with different length, an example would be appending a number in front of the vector



      let addElementInfront (x:Vector<float>) =
      x.ToArray()
      |> Array.append [|x.[0]|]
      |> vector


      Now I want to apply this to all the rows of a (2x2) matrix and I would expect a (2x3) matrix, I tried to use the Matrix.mapRows of MathNet.Numerics.LinearAlgebra but it gives me error that the size needs to be the same.



      Just wonder if MathNet has any other function to map rows that results a different size matrix.



      Thanks.







      f# mathnet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 7:25









      Jose VuJose Vu

      788




      788
























          1 Answer
          1






          active

          oldest

          votes


















          3














          It seems you are trying to duplicate the first column of the matrix. For example:



          1.0; 2.0         1.0; 1.0; 2.0
          3.0; 4.0 becomes 3.0; 3.0; 4.0


          If this is true, then the code could be:



          let m = matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          m
          |> Matrix.prependCol (m.Column 0)


          UPDATE



          Because the assumption above is not true.



          So you can get the seq of the matrix rows, then transform it as usual with Seq.map, and finally make the result matrix:



          let transform f m =
          m
          |> Matrix.toRowSeq
          |> Seq.map f
          |> matrix

          // or even shorter in F# idiomatic style:
          let transform f =
          Matrix.toRowSeq >> Seq.map f >> matrix

          // test

          let addElementInFront (x : Vector<float>) =
          x.ToArray()
          |> Array.append [| x.[0] |]
          |> vector

          matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          |> transform addElementInFront





          share|improve this answer





















          • 1





            hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

            – Jose Vu
            Nov 14 '18 at 9:10








          • 1





            Yes, edited my answer.

            – Nghia Bui
            Nov 14 '18 at 10:03











          • wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

            – Jose Vu
            Nov 14 '18 at 23:50













          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',
          autoActivateHeartbeat: false,
          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%2f53295029%2ff-math-net-matrix-maprows-to-create-new-matrix-with-different-size%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          It seems you are trying to duplicate the first column of the matrix. For example:



          1.0; 2.0         1.0; 1.0; 2.0
          3.0; 4.0 becomes 3.0; 3.0; 4.0


          If this is true, then the code could be:



          let m = matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          m
          |> Matrix.prependCol (m.Column 0)


          UPDATE



          Because the assumption above is not true.



          So you can get the seq of the matrix rows, then transform it as usual with Seq.map, and finally make the result matrix:



          let transform f m =
          m
          |> Matrix.toRowSeq
          |> Seq.map f
          |> matrix

          // or even shorter in F# idiomatic style:
          let transform f =
          Matrix.toRowSeq >> Seq.map f >> matrix

          // test

          let addElementInFront (x : Vector<float>) =
          x.ToArray()
          |> Array.append [| x.[0] |]
          |> vector

          matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          |> transform addElementInFront





          share|improve this answer





















          • 1





            hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

            – Jose Vu
            Nov 14 '18 at 9:10








          • 1





            Yes, edited my answer.

            – Nghia Bui
            Nov 14 '18 at 10:03











          • wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

            – Jose Vu
            Nov 14 '18 at 23:50


















          3














          It seems you are trying to duplicate the first column of the matrix. For example:



          1.0; 2.0         1.0; 1.0; 2.0
          3.0; 4.0 becomes 3.0; 3.0; 4.0


          If this is true, then the code could be:



          let m = matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          m
          |> Matrix.prependCol (m.Column 0)


          UPDATE



          Because the assumption above is not true.



          So you can get the seq of the matrix rows, then transform it as usual with Seq.map, and finally make the result matrix:



          let transform f m =
          m
          |> Matrix.toRowSeq
          |> Seq.map f
          |> matrix

          // or even shorter in F# idiomatic style:
          let transform f =
          Matrix.toRowSeq >> Seq.map f >> matrix

          // test

          let addElementInFront (x : Vector<float>) =
          x.ToArray()
          |> Array.append [| x.[0] |]
          |> vector

          matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          |> transform addElementInFront





          share|improve this answer





















          • 1





            hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

            – Jose Vu
            Nov 14 '18 at 9:10








          • 1





            Yes, edited my answer.

            – Nghia Bui
            Nov 14 '18 at 10:03











          • wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

            – Jose Vu
            Nov 14 '18 at 23:50
















          3












          3








          3







          It seems you are trying to duplicate the first column of the matrix. For example:



          1.0; 2.0         1.0; 1.0; 2.0
          3.0; 4.0 becomes 3.0; 3.0; 4.0


          If this is true, then the code could be:



          let m = matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          m
          |> Matrix.prependCol (m.Column 0)


          UPDATE



          Because the assumption above is not true.



          So you can get the seq of the matrix rows, then transform it as usual with Seq.map, and finally make the result matrix:



          let transform f m =
          m
          |> Matrix.toRowSeq
          |> Seq.map f
          |> matrix

          // or even shorter in F# idiomatic style:
          let transform f =
          Matrix.toRowSeq >> Seq.map f >> matrix

          // test

          let addElementInFront (x : Vector<float>) =
          x.ToArray()
          |> Array.append [| x.[0] |]
          |> vector

          matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          |> transform addElementInFront





          share|improve this answer















          It seems you are trying to duplicate the first column of the matrix. For example:



          1.0; 2.0         1.0; 1.0; 2.0
          3.0; 4.0 becomes 3.0; 3.0; 4.0


          If this is true, then the code could be:



          let m = matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          m
          |> Matrix.prependCol (m.Column 0)


          UPDATE



          Because the assumption above is not true.



          So you can get the seq of the matrix rows, then transform it as usual with Seq.map, and finally make the result matrix:



          let transform f m =
          m
          |> Matrix.toRowSeq
          |> Seq.map f
          |> matrix

          // or even shorter in F# idiomatic style:
          let transform f =
          Matrix.toRowSeq >> Seq.map f >> matrix

          // test

          let addElementInFront (x : Vector<float>) =
          x.ToArray()
          |> Array.append [| x.[0] |]
          |> vector

          matrix [ [ 1.0; 2.0 ]
          [ 3.0; 4.0 ] ]
          |> transform addElementInFront






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 7:10

























          answered Nov 14 '18 at 8:51









          Nghia BuiNghia Bui

          1,463813




          1,463813








          • 1





            hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

            – Jose Vu
            Nov 14 '18 at 9:10








          • 1





            Yes, edited my answer.

            – Nghia Bui
            Nov 14 '18 at 10:03











          • wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

            – Jose Vu
            Nov 14 '18 at 23:50
















          • 1





            hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

            – Jose Vu
            Nov 14 '18 at 9:10








          • 1





            Yes, edited my answer.

            – Nghia Bui
            Nov 14 '18 at 10:03











          • wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

            – Jose Vu
            Nov 14 '18 at 23:50










          1




          1





          hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

          – Jose Vu
          Nov 14 '18 at 9:10







          hi Nghia, thanks for your comment. The case I laid out is just an example that the function will transform the size of the vector, it needs not to be the exact. My question was to find the mapRows function that map this function Vector<float>->Vector<float> to Matrix<float>->Matrix<float> where the result matrix has different size than the original.

          – Jose Vu
          Nov 14 '18 at 9:10






          1




          1





          Yes, edited my answer.

          – Nghia Bui
          Nov 14 '18 at 10:03





          Yes, edited my answer.

          – Nghia Bui
          Nov 14 '18 at 10:03













          wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

          – Jose Vu
          Nov 14 '18 at 23:50







          wow it works like a charm, i didn't know the toRowsSeq, also didn't know seq.map can apply on vector. Thanks bro, I marked your comment as the answer

          – Jose Vu
          Nov 14 '18 at 23:50




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295029%2ff-math-net-matrix-maprows-to-create-new-matrix-with-different-size%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

          Bressuire

          Vorschmack

          Quarantine