Question

I am trying to find the index in Date3, a column vector of date numbers from 01/01/2008 to 01/31/2014 that has been repeated many times, matches day. I basically want to organize idx into a cell array like idx{i} in which each cell is one day (So return all the indexes where Date3 equals day, where day is one of the days between 01/01/2008 and 01/31/2014. Eventually, I want to pull out the data for each day by applying the index I found to the variable Data2 (Reshape Data2 so that instead of a long column vector of data concentrations, I'll have a cell array in which each cell is all the data from one day)

This is what I have been doing:

for day = datenum(2008,01,01):1:datenum(2014,01,31); % All the days under consideration
    idx = find(Date3, day); % Index of where Date3 equals the day under consideration
    Data_PM25 = Data2(idx); % Pull out the data based on the idx
end

Example: If Date3 looks like the following (It's actually much larger and repeats many many more times)

  733408
  733409
  733410
  733411
  733412
  733413
  733414
  733415
  733416
  733417
  733418
  733419
  733420
  733421
  733408
  733409
  733410
  733411
  733412
  733413
  733414
  733415
  733416
  733417
  733418
  733419
  733420
  733421

I want idx to be

`idx{1}` = (1, 15) % Where 733408 repeats
`idx{2}` = (2, 16) % Where 733409 repeats
...

And then Data2, which looked like:

    [NaN]
    [NaN]
    [NaN]
    [NaN]
    [NaN]
    [NaN]
    [NaN]
    [NaN]
    [NaN]
    [NaN]
    '25.8'
    '26.1'
    '28.9'
    '37.5'
    '25.2'
    '20' 
    '32.3'
    '41' 
    '46.7'
    '28.2'
    '34.5'
    '31.8'
    '37.6'
    '45.5'
    '54.9'
    '54.8'
    '36.3'
    '18.5'

Will now look like

'Data_PM25{1}' = ([NaN], '25.2')
'Data_PM25{2}' = ([NaN], '20')

...

Of course, the actual outputs will be much longer than just two matches.

What appears to be happening though is that I am comparing every day against Date3, a list of days, so I am getting all the days back.

This question expands off of a previous question: Find where a value matches and concatentate into column vector MATLAB

Was it helpful?

Solution

Use find(ismember…) to find where a certain day shows up in the long column of Date3 and then used that to pull out all the data, Lat, and Lon from that day.

group2cell for some reason, created double the number of days there were supposed to be. Basically, it somehow pulled out two years in which the first year it pulled out (1:365 or 1:366) was random.

day = datenum(years(y), 01, 01):datenum(years(y), 12, 31); % Create a column vector of all days in one year
for d = 1:length(day)
    % Find index where Date3 matches one day, one day at a time
    ind{d} = find(ismember(datenum(Date3), day(d)) == 1);
    data_O3{d} = Data2(ind{d});
end

OTHER TIPS

How about this: The result should be a cell array of vectors, each vector corresponding to the data from a given day.

This of course assumes Date3 and Data2 are the same size

 Date3=[733408
  733409
  733410
  733411
  733412
  733413
  733414
  733415
  733416
  733417
  733418
  733419
  733420
  733421
  733408
  733409
  733410
  733411
  733412
  733413
  733414
  733415
  733416
  733417
  733418
  733419
  733420
  733421];
Data2 = Date3.*50; % //just some dummy data for testing
Data_pm25=cell(0);
for day=datenum(2008,01,01):datenum(2014,01,01)
    idx=day==Date3;
    Data_pm25 = [Data_pm25,Data2(idx)];
end

The only problem I can see with this is if you don't have data for every day, you wouldn't necessarily know which day each cell is representing. This could easily be solved by storing a vector of dates with data.

As I mentioned in your other question try GROUP2CELL function from FileExchange.

You can use it on index as

[newidx, uniqueDate] = group2cell(idx,Date2);

or directly on data

[Data_PM25, uniqueDate] = group2cell(Data2,Date2);

uniqueDate will be an array of all unique Dates. uniqueDate(i) will correspond to Data_PM25(i).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top