Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dukebound85

macrumors Core
Original poster
Jul 17, 2005
19,218
4,342
5045 feet above sea level
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
 
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.
 
Last edited:
This is Matlab btw
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
 
Last edited:
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

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

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:)
 
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
 
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

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
 
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
 
Last edited:
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
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:)
 
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:)

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
 
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
 
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
 
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

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.