question about list creation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I have the following xy list:



list = 19, 21991.4, 63, 5801.71, 71, 5714.21, 99, 2021.72, 135, 
850.353, 165, 1897., 220, 2117.53, 251, 2444.39, 274,
25136.6


I am just interested in the x positions, so I do the following:



x = First[list[[#]]] & /@ Range[Length[list]]


which gets me: 19, 63, 71, 99, 135, 165, 220, 251, 274



Now I would like to create the following lists:



  1. A list where there are zeros everywhere expect for the positions given at x, so something like this: 0,0,0,0,...1 (at position 19), 0,0,... 1 (at position 63)...


  2. A list where I have 1,1,1,1, until position 19, then 2,2,2,2 until position 63 then 3,3,3,3 until position 71 etc.


How could I achieve that ?










share|improve this question

















  • 1




    You can also use x=First/@list
    – Okkes Dulgerci
    Aug 20 at 6:09














up vote
5
down vote

favorite












I have the following xy list:



list = 19, 21991.4, 63, 5801.71, 71, 5714.21, 99, 2021.72, 135, 
850.353, 165, 1897., 220, 2117.53, 251, 2444.39, 274,
25136.6


I am just interested in the x positions, so I do the following:



x = First[list[[#]]] & /@ Range[Length[list]]


which gets me: 19, 63, 71, 99, 135, 165, 220, 251, 274



Now I would like to create the following lists:



  1. A list where there are zeros everywhere expect for the positions given at x, so something like this: 0,0,0,0,...1 (at position 19), 0,0,... 1 (at position 63)...


  2. A list where I have 1,1,1,1, until position 19, then 2,2,2,2 until position 63 then 3,3,3,3 until position 71 etc.


How could I achieve that ?










share|improve this question

















  • 1




    You can also use x=First/@list
    – Okkes Dulgerci
    Aug 20 at 6:09












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I have the following xy list:



list = 19, 21991.4, 63, 5801.71, 71, 5714.21, 99, 2021.72, 135, 
850.353, 165, 1897., 220, 2117.53, 251, 2444.39, 274,
25136.6


I am just interested in the x positions, so I do the following:



x = First[list[[#]]] & /@ Range[Length[list]]


which gets me: 19, 63, 71, 99, 135, 165, 220, 251, 274



Now I would like to create the following lists:



  1. A list where there are zeros everywhere expect for the positions given at x, so something like this: 0,0,0,0,...1 (at position 19), 0,0,... 1 (at position 63)...


  2. A list where I have 1,1,1,1, until position 19, then 2,2,2,2 until position 63 then 3,3,3,3 until position 71 etc.


How could I achieve that ?










share|improve this question













I have the following xy list:



list = 19, 21991.4, 63, 5801.71, 71, 5714.21, 99, 2021.72, 135, 
850.353, 165, 1897., 220, 2117.53, 251, 2444.39, 274,
25136.6


I am just interested in the x positions, so I do the following:



x = First[list[[#]]] & /@ Range[Length[list]]


which gets me: 19, 63, 71, 99, 135, 165, 220, 251, 274



Now I would like to create the following lists:



  1. A list where there are zeros everywhere expect for the positions given at x, so something like this: 0,0,0,0,...1 (at position 19), 0,0,... 1 (at position 63)...


  2. A list where I have 1,1,1,1, until position 19, then 2,2,2,2 until position 63 then 3,3,3,3 until position 71 etc.


How could I achieve that ?







list-manipulation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 19 at 22:33









james

643418




643418







  • 1




    You can also use x=First/@list
    – Okkes Dulgerci
    Aug 20 at 6:09












  • 1




    You can also use x=First/@list
    – Okkes Dulgerci
    Aug 20 at 6:09







1




1




You can also use x=First/@list
– Okkes Dulgerci
Aug 20 at 6:09




You can also use x=First/@list
– Okkes Dulgerci
Aug 20 at 6:09










3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










First of all, you should use Part or its shorthand [[...]] to retrieve list elements, like so:



xvalues = list[[All, 1]]



19, 63, 71, 99, 135, 165, 220, 251, 274




As for list (1), this is often done using SparseArray.



sa = SparseArray[xvalues -> ConstantArray[1, Length[xvalues]], 300];
Normal[sa]


Normal converts the sparse array to a normal list. Any elements that don't have values will be turned into zeros, which is what we want. 300 is the length of the list that you create when you apply Normal to the sparse array.



For list (2), we have to apply Accumulate to this list and also add 1 since it starts counting at 1. This can be done using the sparse array directly or by converting it to a normal list first, it doesn't matter.



Accumulate[sa] + 1





share|improve this answer






















  • Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
    – james
    Aug 19 at 22:46










  • @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
    – C. E.
    Aug 19 at 22:49

















up vote
3
down vote













Here you go:



First Question:



firstList = 
Table[If[MemberQ[list, i], 1, 0], i, 1,
Length[list]]


Second Question:



m = 1
secondList = firstList =
Table[If[MemberQ[list, i], m+1, m], i, 1,
Length[list]]





share|improve this answer




















  • Your list needs to go up to Max[List[[;;1]] rather than length
    – A Simmons
    Aug 19 at 23:42

















up vote
2
down vote













You can create the desired vector using Part by indexing into a vector of all zeros.



xvals = list[[All, 1]];
zeroOne = ConstantArray[0, Max[xvals]];
zeroOne[[xvals]] = 1


So the vector zeroOne now contains ones at just the xvals and zeros everywhere else.






share|improve this answer




















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "387"
    ;
    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: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180270%2fquestion-about-list-creation%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    6
    down vote



    accepted










    First of all, you should use Part or its shorthand [[...]] to retrieve list elements, like so:



    xvalues = list[[All, 1]]



    19, 63, 71, 99, 135, 165, 220, 251, 274




    As for list (1), this is often done using SparseArray.



    sa = SparseArray[xvalues -> ConstantArray[1, Length[xvalues]], 300];
    Normal[sa]


    Normal converts the sparse array to a normal list. Any elements that don't have values will be turned into zeros, which is what we want. 300 is the length of the list that you create when you apply Normal to the sparse array.



    For list (2), we have to apply Accumulate to this list and also add 1 since it starts counting at 1. This can be done using the sparse array directly or by converting it to a normal list first, it doesn't matter.



    Accumulate[sa] + 1





    share|improve this answer






















    • Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
      – james
      Aug 19 at 22:46










    • @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
      – C. E.
      Aug 19 at 22:49














    up vote
    6
    down vote



    accepted










    First of all, you should use Part or its shorthand [[...]] to retrieve list elements, like so:



    xvalues = list[[All, 1]]



    19, 63, 71, 99, 135, 165, 220, 251, 274




    As for list (1), this is often done using SparseArray.



    sa = SparseArray[xvalues -> ConstantArray[1, Length[xvalues]], 300];
    Normal[sa]


    Normal converts the sparse array to a normal list. Any elements that don't have values will be turned into zeros, which is what we want. 300 is the length of the list that you create when you apply Normal to the sparse array.



    For list (2), we have to apply Accumulate to this list and also add 1 since it starts counting at 1. This can be done using the sparse array directly or by converting it to a normal list first, it doesn't matter.



    Accumulate[sa] + 1





    share|improve this answer






















    • Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
      – james
      Aug 19 at 22:46










    • @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
      – C. E.
      Aug 19 at 22:49












    up vote
    6
    down vote



    accepted







    up vote
    6
    down vote



    accepted






    First of all, you should use Part or its shorthand [[...]] to retrieve list elements, like so:



    xvalues = list[[All, 1]]



    19, 63, 71, 99, 135, 165, 220, 251, 274




    As for list (1), this is often done using SparseArray.



    sa = SparseArray[xvalues -> ConstantArray[1, Length[xvalues]], 300];
    Normal[sa]


    Normal converts the sparse array to a normal list. Any elements that don't have values will be turned into zeros, which is what we want. 300 is the length of the list that you create when you apply Normal to the sparse array.



    For list (2), we have to apply Accumulate to this list and also add 1 since it starts counting at 1. This can be done using the sparse array directly or by converting it to a normal list first, it doesn't matter.



    Accumulate[sa] + 1





    share|improve this answer














    First of all, you should use Part or its shorthand [[...]] to retrieve list elements, like so:



    xvalues = list[[All, 1]]



    19, 63, 71, 99, 135, 165, 220, 251, 274




    As for list (1), this is often done using SparseArray.



    sa = SparseArray[xvalues -> ConstantArray[1, Length[xvalues]], 300];
    Normal[sa]


    Normal converts the sparse array to a normal list. Any elements that don't have values will be turned into zeros, which is what we want. 300 is the length of the list that you create when you apply Normal to the sparse array.



    For list (2), we have to apply Accumulate to this list and also add 1 since it starts counting at 1. This can be done using the sparse array directly or by converting it to a normal list first, it doesn't matter.



    Accumulate[sa] + 1






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 19 at 23:01

























    answered Aug 19 at 22:43









    C. E.

    47.7k391193




    47.7k391193











    • Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
      – james
      Aug 19 at 22:46










    • @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
      – C. E.
      Aug 19 at 22:49
















    • Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
      – james
      Aug 19 at 22:46










    • @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
      – C. E.
      Aug 19 at 22:49















    Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
    – james
    Aug 19 at 22:46




    Thanks a lot for the answer. What does the " 300" represent in the sparse Array ?
    – james
    Aug 19 at 22:46












    @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
    – C. E.
    Aug 19 at 22:49




    @james There is a sentence about this in the answer, but I added in an update so you may have to refresh the page to see it.
    – C. E.
    Aug 19 at 22:49










    up vote
    3
    down vote













    Here you go:



    First Question:



    firstList = 
    Table[If[MemberQ[list, i], 1, 0], i, 1,
    Length[list]]


    Second Question:



    m = 1
    secondList = firstList =
    Table[If[MemberQ[list, i], m+1, m], i, 1,
    Length[list]]





    share|improve this answer




















    • Your list needs to go up to Max[List[[;;1]] rather than length
      – A Simmons
      Aug 19 at 23:42














    up vote
    3
    down vote













    Here you go:



    First Question:



    firstList = 
    Table[If[MemberQ[list, i], 1, 0], i, 1,
    Length[list]]


    Second Question:



    m = 1
    secondList = firstList =
    Table[If[MemberQ[list, i], m+1, m], i, 1,
    Length[list]]





    share|improve this answer




















    • Your list needs to go up to Max[List[[;;1]] rather than length
      – A Simmons
      Aug 19 at 23:42












    up vote
    3
    down vote










    up vote
    3
    down vote









    Here you go:



    First Question:



    firstList = 
    Table[If[MemberQ[list, i], 1, 0], i, 1,
    Length[list]]


    Second Question:



    m = 1
    secondList = firstList =
    Table[If[MemberQ[list, i], m+1, m], i, 1,
    Length[list]]





    share|improve this answer












    Here you go:



    First Question:



    firstList = 
    Table[If[MemberQ[list, i], 1, 0], i, 1,
    Length[list]]


    Second Question:



    m = 1
    secondList = firstList =
    Table[If[MemberQ[list, i], m+1, m], i, 1,
    Length[list]]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 19 at 22:43









    henry

    1,130423




    1,130423











    • Your list needs to go up to Max[List[[;;1]] rather than length
      – A Simmons
      Aug 19 at 23:42
















    • Your list needs to go up to Max[List[[;;1]] rather than length
      – A Simmons
      Aug 19 at 23:42















    Your list needs to go up to Max[List[[;;1]] rather than length
    – A Simmons
    Aug 19 at 23:42




    Your list needs to go up to Max[List[[;;1]] rather than length
    – A Simmons
    Aug 19 at 23:42










    up vote
    2
    down vote













    You can create the desired vector using Part by indexing into a vector of all zeros.



    xvals = list[[All, 1]];
    zeroOne = ConstantArray[0, Max[xvals]];
    zeroOne[[xvals]] = 1


    So the vector zeroOne now contains ones at just the xvals and zeros everywhere else.






    share|improve this answer
























      up vote
      2
      down vote













      You can create the desired vector using Part by indexing into a vector of all zeros.



      xvals = list[[All, 1]];
      zeroOne = ConstantArray[0, Max[xvals]];
      zeroOne[[xvals]] = 1


      So the vector zeroOne now contains ones at just the xvals and zeros everywhere else.






      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        You can create the desired vector using Part by indexing into a vector of all zeros.



        xvals = list[[All, 1]];
        zeroOne = ConstantArray[0, Max[xvals]];
        zeroOne[[xvals]] = 1


        So the vector zeroOne now contains ones at just the xvals and zeros everywhere else.






        share|improve this answer












        You can create the desired vector using Part by indexing into a vector of all zeros.



        xvals = list[[All, 1]];
        zeroOne = ConstantArray[0, Max[xvals]];
        zeroOne[[xvals]] = 1


        So the vector zeroOne now contains ones at just the xvals and zeros everywhere else.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 20 at 0:22









        bill s

        51.1k373145




        51.1k373145



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180270%2fquestion-about-list-creation%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?