Why am I getting a vertcat error? (Matlab)












1















I am trying to plot the first row of my matrix against time t but I cannot figure out why my matrix yields the error: "vertcat:
Dimensions of matrices being concatenated are not consistent." enter image description here



t = linspace(0,100);

y_mat = (1./t).*([1, t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
(3/2)*(exp(-t)-exp(-3*t)), 1-(3/2)*exp(-3*t)+
(1/2)*exp(-t)] * [(t-4)/3;1]);

plot(t,y_mat(1,:))









share|improve this question





























    1















    I am trying to plot the first row of my matrix against time t but I cannot figure out why my matrix yields the error: "vertcat:
    Dimensions of matrices being concatenated are not consistent." enter image description here



    t = linspace(0,100);

    y_mat = (1./t).*([1, t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
    (3/2)*(exp(-t)-exp(-3*t)), 1-(3/2)*exp(-3*t)+
    (1/2)*exp(-t)] * [(t-4)/3;1]);

    plot(t,y_mat(1,:))









    share|improve this question



























      1












      1








      1








      I am trying to plot the first row of my matrix against time t but I cannot figure out why my matrix yields the error: "vertcat:
      Dimensions of matrices being concatenated are not consistent." enter image description here



      t = linspace(0,100);

      y_mat = (1./t).*([1, t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
      (3/2)*(exp(-t)-exp(-3*t)), 1-(3/2)*exp(-3*t)+
      (1/2)*exp(-t)] * [(t-4)/3;1]);

      plot(t,y_mat(1,:))









      share|improve this question
















      I am trying to plot the first row of my matrix against time t but I cannot figure out why my matrix yields the error: "vertcat:
      Dimensions of matrices being concatenated are not consistent." enter image description here



      t = linspace(0,100);

      y_mat = (1./t).*([1, t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
      (3/2)*(exp(-t)-exp(-3*t)), 1-(3/2)*exp(-3*t)+
      (1/2)*exp(-t)] * [(t-4)/3;1]);

      plot(t,y_mat(1,:))






      matlab matrix






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 5:04







      astroball

















      asked Nov 16 '18 at 4:58









      astroballastroball

      5218




      5218
























          3 Answers
          3






          active

          oldest

          votes


















          1














          You can also write it out more explicitly. The equation reads:



          [ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]


          Since each of those terms is a scalar, you can compute them for all t at the same time using element-wise multiplication:



          t = linspace(0,100);

          pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
          pt3 = (3/2)*(exp(-t)-exp(-3*t));
          pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);

          pt5 = (t-4)/3;

          y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];

          plot(t,y_mat)


          This might be a bit more verbose, but I don't think it's any less readable than other solutions. And it is much more efficient: 0.0571 ms, versus 483.3 ms (syms solution) and 0.681 ms (loop solution), for a t with 500 elements.



          (Note that multiplying by 1./t uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun.)






          share|improve this answer































            3














            You are thinking in the term of symbolic notation but implementing in matrix notation. When you do t = linspace(0,100); it creates a 1x100 matrix (array). So when later on it is used in the definition of y_mat, each expression used in the definition evaluates to 1x100 matrix. So your y_mat definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] which obviously fails.



            You have two options:
            Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1s are appropriately replicated).



            OR



            use Matlabs's symbolic variables and expressions probably like this :



            syms t  % creating symbolic variable
            % creating symbolic expressions
            f0 = 1/t
            f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
            f2 = (3/2)*(exp(-t)-exp(-3*t));
            f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
            f4 = (t-4)/3;
            % defining y_mat
            y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]

            % putting value in symbolic variable
            t = linspace(eps,100); % eps to avoid division by 0 error

            % substitute values and evaluate y_mat
            y_mat_vals = eval(subs(y_mat));


            This gives y_mat_vals a 2x100 matrix, as the answer.






            share|improve this answer

































              1














              YOu have messed up your code..you need to be careful when typing such functions. To make it simple, I have used a loop.



              t = linspace(0,100);

              nt = length(t) ;
              y_mat = zeros(2,nt) ;

              for i = 1:nt
              y_mat(:,i) = (1/t(i))*([1 t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
              (3/2)*(exp(-t(i))-exp(-3*t(i))) 1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
              end
              plot(t,y_mat)


              enter image description here






              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',
                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%2f53331699%2fwhy-am-i-getting-a-vertcat-error-matlab%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                You can also write it out more explicitly. The equation reads:



                [ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]


                Since each of those terms is a scalar, you can compute them for all t at the same time using element-wise multiplication:



                t = linspace(0,100);

                pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                pt3 = (3/2)*(exp(-t)-exp(-3*t));
                pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);

                pt5 = (t-4)/3;

                y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];

                plot(t,y_mat)


                This might be a bit more verbose, but I don't think it's any less readable than other solutions. And it is much more efficient: 0.0571 ms, versus 483.3 ms (syms solution) and 0.681 ms (loop solution), for a t with 500 elements.



                (Note that multiplying by 1./t uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun.)






                share|improve this answer




























                  1














                  You can also write it out more explicitly. The equation reads:



                  [ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]


                  Since each of those terms is a scalar, you can compute them for all t at the same time using element-wise multiplication:



                  t = linspace(0,100);

                  pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                  pt3 = (3/2)*(exp(-t)-exp(-3*t));
                  pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);

                  pt5 = (t-4)/3;

                  y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];

                  plot(t,y_mat)


                  This might be a bit more verbose, but I don't think it's any less readable than other solutions. And it is much more efficient: 0.0571 ms, versus 483.3 ms (syms solution) and 0.681 ms (loop solution), for a t with 500 elements.



                  (Note that multiplying by 1./t uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun.)






                  share|improve this answer


























                    1












                    1








                    1







                    You can also write it out more explicitly. The equation reads:



                    [ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]


                    Since each of those terms is a scalar, you can compute them for all t at the same time using element-wise multiplication:



                    t = linspace(0,100);

                    pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                    pt3 = (3/2)*(exp(-t)-exp(-3*t));
                    pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);

                    pt5 = (t-4)/3;

                    y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];

                    plot(t,y_mat)


                    This might be a bit more verbose, but I don't think it's any less readable than other solutions. And it is much more efficient: 0.0571 ms, versus 483.3 ms (syms solution) and 0.681 ms (loop solution), for a t with 500 elements.



                    (Note that multiplying by 1./t uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun.)






                    share|improve this answer













                    You can also write it out more explicitly. The equation reads:



                    [ 1,pt2 ; pt3,pt4 ] * [ pt5 ; 1 ] = [ pt5 + pt2 ; pt3.*pt5 + pt4 ]


                    Since each of those terms is a scalar, you can compute them for all t at the same time using element-wise multiplication:



                    t = linspace(0,100);

                    pt2 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                    pt3 = (3/2)*(exp(-t)-exp(-3*t));
                    pt4 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);

                    pt5 = (t-4)/3;

                    y_mat = (1./t) .* [ pt5 + pt2 ; pt3.*pt5 + pt4 ];

                    plot(t,y_mat)


                    This might be a bit more verbose, but I don't think it's any less readable than other solutions. And it is much more efficient: 0.0571 ms, versus 483.3 ms (syms solution) and 0.681 ms (loop solution), for a t with 500 elements.



                    (Note that multiplying by 1./t uses implicit singleton expansion. This works in MATLAB R2016b and newer. For older versions of MATLAB, use bsxfun.)







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 7:58









                    Cris LuengoCris Luengo

                    22.1k52253




                    22.1k52253

























                        3














                        You are thinking in the term of symbolic notation but implementing in matrix notation. When you do t = linspace(0,100); it creates a 1x100 matrix (array). So when later on it is used in the definition of y_mat, each expression used in the definition evaluates to 1x100 matrix. So your y_mat definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] which obviously fails.



                        You have two options:
                        Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1s are appropriately replicated).



                        OR



                        use Matlabs's symbolic variables and expressions probably like this :



                        syms t  % creating symbolic variable
                        % creating symbolic expressions
                        f0 = 1/t
                        f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                        f2 = (3/2)*(exp(-t)-exp(-3*t));
                        f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
                        f4 = (t-4)/3;
                        % defining y_mat
                        y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]

                        % putting value in symbolic variable
                        t = linspace(eps,100); % eps to avoid division by 0 error

                        % substitute values and evaluate y_mat
                        y_mat_vals = eval(subs(y_mat));


                        This gives y_mat_vals a 2x100 matrix, as the answer.






                        share|improve this answer






























                          3














                          You are thinking in the term of symbolic notation but implementing in matrix notation. When you do t = linspace(0,100); it creates a 1x100 matrix (array). So when later on it is used in the definition of y_mat, each expression used in the definition evaluates to 1x100 matrix. So your y_mat definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] which obviously fails.



                          You have two options:
                          Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1s are appropriately replicated).



                          OR



                          use Matlabs's symbolic variables and expressions probably like this :



                          syms t  % creating symbolic variable
                          % creating symbolic expressions
                          f0 = 1/t
                          f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                          f2 = (3/2)*(exp(-t)-exp(-3*t));
                          f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
                          f4 = (t-4)/3;
                          % defining y_mat
                          y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]

                          % putting value in symbolic variable
                          t = linspace(eps,100); % eps to avoid division by 0 error

                          % substitute values and evaluate y_mat
                          y_mat_vals = eval(subs(y_mat));


                          This gives y_mat_vals a 2x100 matrix, as the answer.






                          share|improve this answer




























                            3












                            3








                            3







                            You are thinking in the term of symbolic notation but implementing in matrix notation. When you do t = linspace(0,100); it creates a 1x100 matrix (array). So when later on it is used in the definition of y_mat, each expression used in the definition evaluates to 1x100 matrix. So your y_mat definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] which obviously fails.



                            You have two options:
                            Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1s are appropriately replicated).



                            OR



                            use Matlabs's symbolic variables and expressions probably like this :



                            syms t  % creating symbolic variable
                            % creating symbolic expressions
                            f0 = 1/t
                            f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                            f2 = (3/2)*(exp(-t)-exp(-3*t));
                            f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
                            f4 = (t-4)/3;
                            % defining y_mat
                            y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]

                            % putting value in symbolic variable
                            t = linspace(eps,100); % eps to avoid division by 0 error

                            % substitute values and evaluate y_mat
                            y_mat_vals = eval(subs(y_mat));


                            This gives y_mat_vals a 2x100 matrix, as the answer.






                            share|improve this answer















                            You are thinking in the term of symbolic notation but implementing in matrix notation. When you do t = linspace(0,100); it creates a 1x100 matrix (array). So when later on it is used in the definition of y_mat, each expression used in the definition evaluates to 1x100 matrix. So your y_mat definition is tying to do this : [1x100] * [1 1x100 ; 1x100 1x100] * [1x100 ; 1] which obviously fails.



                            You have two options:
                            Do all computations in the matrix notation by first computing the matrix multiplication separately and restructuring the matrices to represent the actual multiplication (ensure the 1s are appropriately replicated).



                            OR



                            use Matlabs's symbolic variables and expressions probably like this :



                            syms t  % creating symbolic variable
                            % creating symbolic expressions
                            f0 = 1/t
                            f1 = t+(1/2)*exp(-3*t)-(1/2)*exp(-t);
                            f2 = (3/2)*(exp(-t)-exp(-3*t));
                            f3 = 1-(3/2)*exp(-3*t)+(1/2)*exp(-t);
                            f4 = (t-4)/3;
                            % defining y_mat
                            y_mat = f0 * [1 f1; f2 f3] * [f4 ; 1]

                            % putting value in symbolic variable
                            t = linspace(eps,100); % eps to avoid division by 0 error

                            % substitute values and evaluate y_mat
                            y_mat_vals = eval(subs(y_mat));


                            This gives y_mat_vals a 2x100 matrix, as the answer.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 16 '18 at 8:23

























                            answered Nov 16 '18 at 7:07









                            Saurabh SainiSaurabh Saini

                            186110




                            186110























                                1














                                YOu have messed up your code..you need to be careful when typing such functions. To make it simple, I have used a loop.



                                t = linspace(0,100);

                                nt = length(t) ;
                                y_mat = zeros(2,nt) ;

                                for i = 1:nt
                                y_mat(:,i) = (1/t(i))*([1 t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
                                (3/2)*(exp(-t(i))-exp(-3*t(i))) 1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
                                end
                                plot(t,y_mat)


                                enter image description here






                                share|improve this answer




























                                  1














                                  YOu have messed up your code..you need to be careful when typing such functions. To make it simple, I have used a loop.



                                  t = linspace(0,100);

                                  nt = length(t) ;
                                  y_mat = zeros(2,nt) ;

                                  for i = 1:nt
                                  y_mat(:,i) = (1/t(i))*([1 t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
                                  (3/2)*(exp(-t(i))-exp(-3*t(i))) 1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
                                  end
                                  plot(t,y_mat)


                                  enter image description here






                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    YOu have messed up your code..you need to be careful when typing such functions. To make it simple, I have used a loop.



                                    t = linspace(0,100);

                                    nt = length(t) ;
                                    y_mat = zeros(2,nt) ;

                                    for i = 1:nt
                                    y_mat(:,i) = (1/t(i))*([1 t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
                                    (3/2)*(exp(-t(i))-exp(-3*t(i))) 1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
                                    end
                                    plot(t,y_mat)


                                    enter image description here






                                    share|improve this answer













                                    YOu have messed up your code..you need to be careful when typing such functions. To make it simple, I have used a loop.



                                    t = linspace(0,100);

                                    nt = length(t) ;
                                    y_mat = zeros(2,nt) ;

                                    for i = 1:nt
                                    y_mat(:,i) = (1/t(i))*([1 t(i)+(1/2)*exp(-3*t(i))-(1/2)*exp(-t(i));
                                    (3/2)*(exp(-t(i))-exp(-3*t(i))) 1-(3/2)*exp(-3*t(i))+(1/2)*exp(-t(i))])*[(t(i)-4)/3;1];
                                    end
                                    plot(t,y_mat)


                                    enter image description here







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 16 '18 at 7:10









                                    Siva Srinivas KolukulaSiva Srinivas Kolukula

                                    1,1351613




                                    1,1351613






























                                        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%2f53331699%2fwhy-am-i-getting-a-vertcat-error-matlab%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