How to grab certain indexes out of an array?

Discussion in 'Mac Programming' started by dukebound85, Jan 19, 2011.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    I am trying to write a code that will grab certain indexes out of an array.

    Essentially, I have created an array of 612 indexes, with the first index being January, Feb,through December and so on repeatedly. I now would like to grab only every Jan, Feb, and Dec resulting in 153 indexes into a new array. Here is what I have at the moment. Not achieving what I want but I feel it is close

    Code:
    mean_winter_global = zeros(153,1);
    winter_enso = zeros(153,1);
    
    mean__winter_global(1) = mean_global(1);
    mean__winter_global(2) = mean_global(2);
    for i = 3:612
        for k = 3:153
            if mean_global(i)==mean_global(12*k) && i<613
                mean_winter_global(k) = mean_global(i);
            elseif  mean_global(i)==mean_global(13*k) && i<612
                    mean_winter_global(k) = mean_global(i);
             elseif  mean_global(i)==mean_global(14*k) && i<611
                     mean_winter_global(k) = mean_global(i);
            end
        end
    end
    Any obvious flaws woud be awesome. Thanks for any help

    This is Matlab btw
     
  2. chown33, Jan 19, 2011
    Last edited: Jan 19, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #2
    I'm not sure how you want the output array ordered, or if it matters.

    To get it ordered by year, I'd have a source-array index that starts at 12 (or whatever array-index represents December of first year), then takes the next 3 sequential items from the source array. This will get you Dec of year N and Jan and Feb of year N+1. Advance index by 12. Repeat until exhausted.

    The output-array index is simply incremented by 1 for each item from the source-array. Oh, and Jan + Feb of first year are special cases, as you've shown, so handle those outside the loop.

    Or start the source index at 13 (Jan of 2nd year in source-array), then take 3 items at index-12, index-11, and index-1. Inc source index by 12. Repeat. If it's not clear what this does, walk through it on paper.

    Nested loops and if/elseif chains seems wrong or overly complex.
     
  3. balamw, Jan 19, 2011
    Last edited: Jan 19, 2011

    balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    Matlab makes extracting subarrays super easy.
    e.g.

    mean_global(1:12:end) will give you every 12th element without a loop.

    EDIT: here's how I would do it.

    Code:
    maxind=length(mean_global);
    index_jan= 1:12:maxind;
    index_feb= 2:12:maxind;
    index_dec=12:12:maxind;
    index_winter=sort([index_jan, index_feb, index_dec]);
    mean_winter_global = mean_global(index_winter);
    
    EDIT 2: There are many, many ways of doing this in Matlab. Another way you might consider is reshaping the data. (doc reshape http://www.mathworks.com/help/techdoc/ref/reshape.html) Take your 612 array and turn it into a 51x12 array and use ( 1, : ) to pull out January.

    B
     
  4. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #4
    balamw gave you a feasible solution but I recommend reading a quick tutorial on matlab syntax before going forwards.
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    Here's a link to a slightly more advanced part of the documentation that I found particularly illuminating. http://www.mathworks.com/support/tech-notes/1100/1109.html

    It led me to a rule for my own matlab code. "Avoid loops where possible, vectorize instead!"

    B
     
  6. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #6
    Thanks! There is always an easier way to do something. I will definitely be looking to use this vector format as opposed to loops.

    Thanks alot:)
     
  7. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #7
    I have a follow up question

    say I have a matrix [aaabbbcccdddeee;aaabbbcccdddeee;etc]

    How can I get the mean of the "a" indexes, then the "b" indexes, etc for each row ?

    Thanks for any insight

    I would imagine it is similar to above in that instead of every first element grabed every 12th time, to grab the first 3 elements every 3rd time in my example
     
  8. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    try
    Code:
    matrix(1:3,:) or matrix(:,1:3)
    I can't remember the right order right now.

    Code:
    matrix(4:6,:) or matrix (:,4:6)
    will do the "b"s etc...


    B
     
  9. dukebound85, Jan 24, 2011
    Last edited: Jan 24, 2011

    dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #9
    Thanks for the help

    I have tried to incorporate your method somewhat to achieve my goal. Here it is in the event you were interested!
    Code:
    for i = 1:1:36                                                       %This loop breaks the surf_temp data into lat and longtitude values. ie, converts the 2D matrix into 3D
        surf_temp_3d(:,i,:)=surf_temp(:,(i-1)*72+1:(i-1)*72+72);
     end
    Balamw, you are a great help. I really appreciate it
     
  10. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #10
    You're welcome.

    Play with the reshape function some. You can probably do what you need with that. I had something similar that could be handled by two calls to reshape.

    B
     
  11. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #11
    Another question:eek:

    I have been playing with the index method in application to a 3d array (153,36,72)
    Is there a straightforward method like above to grab every 12 index but also keep the other 2 dimensions while doing that?

    I have tried
    Code:
    index_jan1 = surf_temp(1:12:maxind,1);
    but this method seems to throw away the other 2 indexes that accompany it....as expected. I have tried to retain the other dimensions via colons but I hav't been able to figure out any syntax that works. Thanks for any input, as always:)
     
  12. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #12
    Code:
    index_jan1 = surf_temp(1:12:end,:,:);
    Should do what I think you want. I tried it in octave. Built a 153,36,72 array and extracted 12x36x72 from it with the data intact.

    Did you forget a comma or two?

    B
     
  13. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #13
    I must have as that worked. Though, when I try to sort into a 153x36x72 matrix, it sorts it as a 51x108x72 matrix. Any reason my it is combing the second index when the first is desired?
    Thanks
     
  14. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    How are you sorting?

    B
     
  15. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #15
    The same way as you enlightened me earlier. I was able to index the desired sequences but now they are a 3d array and no longer a 1d and when I sort, it is sorting the wrong index
     
  16. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #16
    sort has a second argument for which dimension to use.

    http://www.mathworks.com/help/techdoc/ref/sort.html

    mess around with that, but if you don't get it to work, post the code that isn't working.

    B
     
  17. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #17
    This is what I am trying to play with
    Code:
    index_jan1 = surf_temp_3d(1:12:end,:,:);
    index_feb1 = surf_temp_3d(2:12:end,:,:);
    index_dec1 = surf_temp_3d(12:12:end,:,:);
    index_winter_3d = sort([index_jan1,1], [index_feb1,1], [index_dec1,1]);
    Essentially, each index has 51x36x72 elements. I would like to order them in sequential order to give a final matrix of 153x36x72 with the sort preserving the second and third dimensions that are associated with the first
     
  18. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #18
    I get it now.

    The problem is that in index_* you are actually getting the 3D matrix elements and you only need to sort the one dimensional array of indices to pull everything out preserving order.

    Try this.

    Code:
    maxind=size(surf_temp_3d,1);
    index_jan1 = 1:12:maxind;
    index_feb1 = 2:12:maxind;
    index_dec1 = 12:12:maxind;
    index_winter = sort([index_jan1, index_feb1, index_dec1]);
    winter_surf_temp_3d=surf_temp_3d(index_winter,:,:);
    
    B
     

Share This Page