Alter number sort / number sequence












4














I have an array / number sequence a=[1,2,3,4,5] and I'm trying
to create an array / number sequence that looks like a_new below:
The columns represent the orders / index the numbers should go in.



a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]


My thoughts where to use circshift but quickly found out that would not work.



a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end


produces



a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]


Any thoughts? It doesn't need to use circshift if that won't work.



PS: I'm using Octave 4.2 which is similar to Matlab










share|improve this question
























  • can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want? circshift wont work because of this, the first column is a special case
    – Ander Biguri
    Nov 13 at 17:11










  • @AnderBiguri the columns represent the orders / index the numbers should go in.
    – Rick T
    Nov 13 at 17:17
















4














I have an array / number sequence a=[1,2,3,4,5] and I'm trying
to create an array / number sequence that looks like a_new below:
The columns represent the orders / index the numbers should go in.



a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]


My thoughts where to use circshift but quickly found out that would not work.



a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end


produces



a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]


Any thoughts? It doesn't need to use circshift if that won't work.



PS: I'm using Octave 4.2 which is similar to Matlab










share|improve this question
























  • can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want? circshift wont work because of this, the first column is a special case
    – Ander Biguri
    Nov 13 at 17:11










  • @AnderBiguri the columns represent the orders / index the numbers should go in.
    – Rick T
    Nov 13 at 17:17














4












4








4







I have an array / number sequence a=[1,2,3,4,5] and I'm trying
to create an array / number sequence that looks like a_new below:
The columns represent the orders / index the numbers should go in.



a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]


My thoughts where to use circshift but quickly found out that would not work.



a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end


produces



a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]


Any thoughts? It doesn't need to use circshift if that won't work.



PS: I'm using Octave 4.2 which is similar to Matlab










share|improve this question















I have an array / number sequence a=[1,2,3,4,5] and I'm trying
to create an array / number sequence that looks like a_new below:
The columns represent the orders / index the numbers should go in.



a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]


My thoughts where to use circshift but quickly found out that would not work.



a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end


produces



a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]


Any thoughts? It doesn't need to use circshift if that won't work.



PS: I'm using Octave 4.2 which is similar to Matlab







matlab sequence octave






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 17:28









Cris Luengo

18.6k51947




18.6k51947










asked Nov 13 at 17:00









Rick T

1,52042971




1,52042971












  • can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want? circshift wont work because of this, the first column is a special case
    – Ander Biguri
    Nov 13 at 17:11










  • @AnderBiguri the columns represent the orders / index the numbers should go in.
    – Rick T
    Nov 13 at 17:17


















  • can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want? circshift wont work because of this, the first column is a special case
    – Ander Biguri
    Nov 13 at 17:11










  • @AnderBiguri the columns represent the orders / index the numbers should go in.
    – Rick T
    Nov 13 at 17:17
















can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want? circshift wont work because of this, the first column is a special case
– Ander Biguri
Nov 13 at 17:11




can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want? circshift wont work because of this, the first column is a special case
– Ander Biguri
Nov 13 at 17:11












@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 at 17:17




@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 at 17:17












3 Answers
3






active

oldest

votes


















4














There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat, toeplitz, tril, and triu:



>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))

a_new =

1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1





share|improve this answer































    1














    I'm not sure about a built-in function, but this should work;



    a=[1,2,3,4,5];
    a_out = ones(length(a), length(a))
    for n=1:5
    a_out(n,:) = [n*ones(n-1),a(n:end)]
    end


    I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!






    share|improve this answer





























      1














      You can use spdiags to generate the matrix:



      n = numel(a);    
      a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);





      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%2f53286059%2falter-number-sort-number-sequence%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









        4














        There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat, toeplitz, tril, and triu:



        >> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))

        a_new =

        1 2 3 4 5
        2 1 2 3 4
        3 3 1 2 3
        4 4 4 1 2
        5 5 5 5 1





        share|improve this answer




























          4














          There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat, toeplitz, tril, and triu:



          >> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))

          a_new =

          1 2 3 4 5
          2 1 2 3 4
          3 3 1 2 3
          4 4 4 1 2
          5 5 5 5 1





          share|improve this answer


























            4












            4








            4






            There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat, toeplitz, tril, and triu:



            >> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))

            a_new =

            1 2 3 4 5
            2 1 2 3 4
            3 3 1 2 3
            4 4 4 1 2
            5 5 5 5 1





            share|improve this answer














            There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat, toeplitz, tril, and triu:



            >> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))

            a_new =

            1 2 3 4 5
            2 1 2 3 4
            3 3 1 2 3
            4 4 4 1 2
            5 5 5 5 1






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 at 17:18

























            answered Nov 13 at 17:11









            gnovice

            115k13229332




            115k13229332

























                1














                I'm not sure about a built-in function, but this should work;



                a=[1,2,3,4,5];
                a_out = ones(length(a), length(a))
                for n=1:5
                a_out(n,:) = [n*ones(n-1),a(n:end)]
                end


                I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!






                share|improve this answer


























                  1














                  I'm not sure about a built-in function, but this should work;



                  a=[1,2,3,4,5];
                  a_out = ones(length(a), length(a))
                  for n=1:5
                  a_out(n,:) = [n*ones(n-1),a(n:end)]
                  end


                  I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!






                  share|improve this answer
























                    1












                    1








                    1






                    I'm not sure about a built-in function, but this should work;



                    a=[1,2,3,4,5];
                    a_out = ones(length(a), length(a))
                    for n=1:5
                    a_out(n,:) = [n*ones(n-1),a(n:end)]
                    end


                    I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!






                    share|improve this answer












                    I'm not sure about a built-in function, but this should work;



                    a=[1,2,3,4,5];
                    a_out = ones(length(a), length(a))
                    for n=1:5
                    a_out(n,:) = [n*ones(n-1),a(n:end)]
                    end


                    I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 at 17:19









                    SRT HellKitty

                    2938




                    2938























                        1














                        You can use spdiags to generate the matrix:



                        n = numel(a);    
                        a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);





                        share|improve this answer


























                          1














                          You can use spdiags to generate the matrix:



                          n = numel(a);    
                          a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);





                          share|improve this answer
























                            1












                            1








                            1






                            You can use spdiags to generate the matrix:



                            n = numel(a);    
                            a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);





                            share|improve this answer












                            You can use spdiags to generate the matrix:



                            n = numel(a);    
                            a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 at 4:04









                            rahnema1

                            9,8752922




                            9,8752922






























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f53286059%2falter-number-sort-number-sequence%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

                                How to pass form data using jquery Ajax to insert data in database?

                                National Museum of Racing and Hall of Fame

                                Guess what letter conforming each word