How to have multidimensional array with different length in Julia











up vote
1
down vote

favorite












I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:



P[1]=[1 2 3 4]
P[2]=[1 4]
P[3]=[8 9 0 0 5 6]
.
.
.









share|improve this question




























    up vote
    1
    down vote

    favorite












    I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:



    P[1]=[1 2 3 4]
    P[2]=[1 4]
    P[3]=[8 9 0 0 5 6]
    .
    .
    .









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:



      P[1]=[1 2 3 4]
      P[2]=[1 4]
      P[3]=[8 9 0 0 5 6]
      .
      .
      .









      share|improve this question















      I need to make a sequence of an array with different length by reading a dataset. I need to call each of them in a loop so probably I need some sort of indexing in order to call them. For example, how can I create the following sequence:



      P[1]=[1 2 3 4]
      P[2]=[1 4]
      P[3]=[8 9 0 0 5 6]
      .
      .
      .






      arrays julia-lang






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 22:03









      marc_s

      566k12610921245




      566k12610921245










      asked Nov 10 at 21:59









      Reabo

      114




      114
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Here it is:



          julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])  
          3-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]

          julia> P[1]
          4-element Array{Int64,1}:
          1
          2
          3
          4

          julia> P[2]
          2-element Array{Int64,1}:
          1
          4

          julia> P[3]
          6-element Array{Int64,1}:
          8
          9
          0
          0
          5
          6


          If you want to add a new element use push!():



          julia> push!(P,[7,8,9])
          4-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]
          [7, 8, 9]





          share|improve this answer























          • Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
            – Reabo
            Nov 10 at 22:12










          • I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
            – Reabo
            Nov 10 at 22:18












          • no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
            – Przemyslaw Szufel
            Nov 10 at 22:18












          • if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
            – Przemyslaw Szufel
            Nov 10 at 22:21








          • 1




            Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
            – DNF
            Nov 11 at 15:56













          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%2f53243831%2fhow-to-have-multidimensional-array-with-different-length-in-julia%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








          up vote
          2
          down vote













          Here it is:



          julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])  
          3-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]

          julia> P[1]
          4-element Array{Int64,1}:
          1
          2
          3
          4

          julia> P[2]
          2-element Array{Int64,1}:
          1
          4

          julia> P[3]
          6-element Array{Int64,1}:
          8
          9
          0
          0
          5
          6


          If you want to add a new element use push!():



          julia> push!(P,[7,8,9])
          4-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]
          [7, 8, 9]





          share|improve this answer























          • Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
            – Reabo
            Nov 10 at 22:12










          • I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
            – Reabo
            Nov 10 at 22:18












          • no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
            – Przemyslaw Szufel
            Nov 10 at 22:18












          • if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
            – Przemyslaw Szufel
            Nov 10 at 22:21








          • 1




            Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
            – DNF
            Nov 11 at 15:56

















          up vote
          2
          down vote













          Here it is:



          julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])  
          3-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]

          julia> P[1]
          4-element Array{Int64,1}:
          1
          2
          3
          4

          julia> P[2]
          2-element Array{Int64,1}:
          1
          4

          julia> P[3]
          6-element Array{Int64,1}:
          8
          9
          0
          0
          5
          6


          If you want to add a new element use push!():



          julia> push!(P,[7,8,9])
          4-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]
          [7, 8, 9]





          share|improve this answer























          • Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
            – Reabo
            Nov 10 at 22:12










          • I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
            – Reabo
            Nov 10 at 22:18












          • no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
            – Przemyslaw Szufel
            Nov 10 at 22:18












          • if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
            – Przemyslaw Szufel
            Nov 10 at 22:21








          • 1




            Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
            – DNF
            Nov 11 at 15:56















          up vote
          2
          down vote










          up vote
          2
          down vote









          Here it is:



          julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])  
          3-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]

          julia> P[1]
          4-element Array{Int64,1}:
          1
          2
          3
          4

          julia> P[2]
          2-element Array{Int64,1}:
          1
          4

          julia> P[3]
          6-element Array{Int64,1}:
          8
          9
          0
          0
          5
          6


          If you want to add a new element use push!():



          julia> push!(P,[7,8,9])
          4-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]
          [7, 8, 9]





          share|improve this answer














          Here it is:



          julia> P = Vector{Vector{Int64}}([[1,2,3,4],[1,4],[8,9,0,0,5,6]])  
          3-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]

          julia> P[1]
          4-element Array{Int64,1}:
          1
          2
          3
          4

          julia> P[2]
          2-element Array{Int64,1}:
          1
          4

          julia> P[3]
          6-element Array{Int64,1}:
          8
          9
          0
          0
          5
          6


          If you want to add a new element use push!():



          julia> push!(P,[7,8,9])
          4-element Array{Array{Int64,1},1}:
          [1, 2, 3, 4]
          [1, 4]
          [8, 9, 0, 0, 5, 6]
          [7, 8, 9]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 22:18

























          answered Nov 10 at 22:09









          Przemyslaw Szufel

          1,239110




          1,239110












          • Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
            – Reabo
            Nov 10 at 22:12










          • I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
            – Reabo
            Nov 10 at 22:18












          • no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
            – Przemyslaw Szufel
            Nov 10 at 22:18












          • if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
            – Przemyslaw Szufel
            Nov 10 at 22:21








          • 1




            Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
            – DNF
            Nov 11 at 15:56




















          • Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
            – Reabo
            Nov 10 at 22:12










          • I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
            – Reabo
            Nov 10 at 22:18












          • no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
            – Przemyslaw Szufel
            Nov 10 at 22:18












          • if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
            – Przemyslaw Szufel
            Nov 10 at 22:21








          • 1




            Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
            – DNF
            Nov 11 at 15:56


















          Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
          – Reabo
          Nov 10 at 22:12




          Thanks, but my problem is that this sequence is very long and I cannot do the first line of your code manually.
          – Reabo
          Nov 10 at 22:12












          I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
          – Reabo
          Nov 10 at 22:18






          I found this and seems it works, thanks V=Vector{Vector{Float64}}(100)
          – Reabo
          Nov 10 at 22:18














          no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
          – Przemyslaw Szufel
          Nov 10 at 22:18






          no problem - I edited my answer. You can create an empty Array by P = Vector{Vector{Int64}}() and then just push! to it.
          – Przemyslaw Szufel
          Nov 10 at 22:18














          if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
          – Przemyslaw Szufel
          Nov 10 at 22:21






          if you know the Array size Vector{Vector{Int64}}(undef, N) and then using P[n]=... is of course better (note the undef)
          – Przemyslaw Szufel
          Nov 10 at 22:21






          1




          1




          Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
          – DNF
          Nov 11 at 15:56






          Initialize empty: P = Vector{Int}. If you already know the entries: P = [[1,2,3,4],[1,4],[8,9,0,0,5,6]]. No need to specify the type in front of the literal.
          – DNF
          Nov 11 at 15:56




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243831%2fhow-to-have-multidimensional-array-with-different-length-in-julia%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

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values