0

I am trying to create a set of 320 matrices, each having dimensions 1152 x 241. Each matrix represents a different time step. I am trying to populate each cell, using a random value from another file. This other file is also dimensioned 1152 x 241, but there are ~2520 time steps from which to choose.

So what is supposed to happen is pick a cell, populate with a value from a random time step from the big file, and move onto the adjacent cell and do the same thing. Repeat until 320 matrices have been created.

Problem is I run the code and I only create one matrix. What do I need to do to fix my code so that 320 matrices are created? Thanks!

clear all;
clc;

% Load datafile
load 1979_1999_tropics_subset_3mmhr.mat

% Create empty maps
rain_fake_timeseries = zeros(1152,241,320);

for i = 1:1152; % set longitude
    %disp(i)
    for j = 1:241; % set latitude
        %disp(j)
        %for k = 1:320; % create map
            %disp(k)
            rain_fake_timeseries = datasample(rain_sample_1979_1999,1,3);
            %disp(rain_fake_timeseries)
            %save random_clus_fake_timeseries.mat rain_fake_timeseries -v7.3;
        %end
    end
end

save random_clus_fake_timeseries.mat rain_fake_timeseries -v7.3;
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Kevin_Q
  • 73
  • 2
  • 8

1 Answers1

1

This is because you are not properly indexing into your time series array to store the data. What you are doing is that you are only saving the last randomly chosen slice in your time series array. If you look at your loop closely, you are simply overwriting the output array at each iteration of the for loop.

You are also not creating your for loop correctly. If I understand you correctly, each location in a slice represents a unique (x,y) coordinate. For each matrix that you have, you want to sample from this exact same location but temporally search through your ~2500 time instances. As such, you need to use all of your loop variables i, j and k to index into your 3D matrix. You also need to access all time slices at position (i,j) and randomly sample from all of the slices. If I can suggest a small optimization change, we can do this with only two for loops rather than three, randomly choose 320 points at this position for all of the time slices, and store it into the 3D matrix.

In other words:

clear all;
clc;

% Load datafile
load 1979_1999_tropics_subset_3mmhr.mat

% Create empty maps
rain_fake_timeseries = zeros(1152,241,320);

for i = 1 : size(rain_fake_timeseries,1)
    for j = 1 : size(rain_fake_timeseries,2)
        rain_fake_timeseries(i,j,:) = datasample(rain_sample_1979_1999(i,j,:), ...
                                      size(rain_fake_timeseries,3), 3);
    end
end

save random_clus_fake_timeseries.mat rain_fake_timeseries -v7.3;

Note that I have replaced the dimensions in the for loop with calls to size so that you can easily change the size of the matrices and it'll still work without you having to change any constants.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Thanks! I am looking to do something a bit different though. I'm not actually taking a random selection of 320 whole time slices from the original ~2500 slices. Instead, I want to build 320 matrices (dimensioned 1152 x 241) but with each adjacent cell populated with a value from a different time slice from the original 2500 slices. – Kevin_Q Jan 23 '15 at 21:40
  • Put another way, each cell represents a set geographic area, so what I am doing is populating one cell with a value from one time, and moving over to the next area and populating it with a value from another time. I want to do this for all 320 of the 1152 x 241 matrices. – Kevin_Q Jan 23 '15 at 21:41
  • Before I edit my answer, I have some questions to ask you, mainly because I'm having trouble understanding your amended problem statement: (1) What does this variable `rain_sample_1979_1999` represent? What do the rows, columns, and slices represent? (2) How do you determine what is a "geographical area" in this 3D array? (3) How do you quantify "time" in this 3D array? This was not made clear in your original question. – rayryeng Jan 23 '15 at 21:48
  • No problem-thanks for your help! (1) rain_sample_1979_1999 contains a rain rate value. (2) As for geographic area, my matrix represents an area stretching across the tropics from 30S to 30N, across the globe (i.e. across 360 degrees longitude). Each grid box is 0.3125 deg longitude by 0.25 degrees latitude. There are 1152 increments of longitude and 241 increments of latitude (1152 x 241 matrix dimensions). (3) Time-I used ~2500 snapshots of the tropics taken over a period of 20 years. So each value per grid cell is an instantaneous rain rate. – Kevin_Q Jan 23 '15 at 22:13
  • Sorry-one more quick comment: (1) is rain rate values, not rain rate value (singular) – Kevin_Q Jan 23 '15 at 22:14
  • OK. I think I get it. Let me describe what you've just told me in a way that I understand and you verify if I'm right. Basically we have unique lat and long coordinates. For one cell, we have one pair of coordinates. You wish to randomly sample from a time slice at this particular coordinate and place this as the output for the one slice. Then, for every slice after this, we look at the same lat and long coordinates but still we randomly sample from a time slice. We repeat this until we have 320 matrices or slices. Am I correct? – rayryeng Jan 23 '15 at 22:33
  • Aha. OK. That requires a bit more work but it is doable. Let me get back to you soon. I'm actually on my way home from work. Stay tuned though! – rayryeng Jan 23 '15 at 22:37
  • @Kevin_Q - Done. Have a look. Good luck! – rayryeng Jan 24 '15 at 01:35
  • Thanks so much-giving it a whirl now! – Kevin_Q Jan 24 '15 at 17:28
  • Let me know. If it works, I wouldn't mind if you accepted my answer. Good luck! – rayryeng Jan 24 '15 at 17:29