2014-03-31 29 views
2

我有大小1X7的細胞,其中的裏面每個小區是365x5xN,其中每個N爲不同的位置(的siteID)出現了。它是根據第5列已經被排序(列是緯度,經度,的siteID,日期和數據)。 (數據可以在這裏找到:https://www.dropbox.com/sh/li3hh1nvt11vok5/4YGfwStQlo變量的問題是PM25)計數的次數東西數量在頂部36行

我想通過整個1X7小區,望着只有前36行(基本上,前10個百分點),算每個日期顯示的次數。換句話說,我想知道數據價值在哪個日子下跌到前10個百分點。

有誰知道我該怎麼做?我無法理解如何處理這個問題 - >在所有這些單元格上計數併爲每年的每一天吐出數量

回答

2

假設你有一個排序的單元陣列,你可以使用這個 -

%%// Get all the dates for all the rows in sorted cell array 
all_dates = []; 
for k1=1:size(sorted_cell,2) 
    all_dates = [all_dates reshape(cell2mat(sorted_cell{1,k1}(:,4,:)),1,[])]; 
end 
all_unique_dates = unique(all_dates); 
all_out = [num2cell(all_unique_dates)' num2cell(zeros(numel(all_unique_dates),1))];%%//' 

%%// Get all the dates for the first 36 rows in sorted cell array 
dates = []; 
for k1=1:size(sorted_cell,2) 
    dates = [dates reshape(cell2mat(sorted_cell{1,k1}(1:36,4,:)),1,[])]; 
end 

%%// Get unique dates and their counts 
unique_dates = unique(dates); 
count = histc(dates, unique_dates); 

%%// As output create a cell array with the first column as dates 
%%// and the second column as the counts 
out = [num2cell(unique_dates)' num2cell(count)'] 

%%// Get all the dates and the corresponding counts. 
%%// Thus many would still have counts as zeros. 
all_out(ismember(all_unique_dates,unique_dates),:)=out; 
+0

謝謝。這工作得很好。但我有個問題。我得到了258天。那是因爲任何零的日子都被遺漏了嗎? – shizishan

+0

是否有可能讓所有的日子,因爲我要畫出他們有差距沿x軸天,將使這更復雜。 – shizishan

+0

@shizishan是的,很可能是因爲我們只選擇第36行。所以,你還是想從第一行36中選擇,但讓所有的日期,只是情節? – Divakar

0

通常,當從外部看起來有些棘手問題時,從而不是內部。我們如何才能從單個數組中獲得頂級日期?

dates = unique(array(1:35,4)); 

現在,如何爲每個細胞做到這一點?環路總是簡單的,但是這是一個非常簡單的功能,讓我們用一行代碼:

datecell = cellfun(@(x) unique(x(1:35,4)), cellarray, 'UniformOutput', false); 

現在我們有一個剛剛我們想要的日期,對於每一個細胞。如果沒有必要讓他們分開,讓我們只堅持他們都連成一個大的數組:

dates = cell2mat(datecell); 
dates = unique(dates); % in case there are any duplicates 

如果你想真正計數每個日期以及(這是一個有點不清楚),它可能是有點太涉及了一個匿名函數,所以我們既可以寫我們自己的函數傳遞給cellfun,或者只是警察出來,並把它貼在一個循環:

dates = {}; 
counts = {}; 
for ii = 1:length(cellarray) 
    [dates{ii}, ~, idx] = unique(cellarray{ii}(1:35,4)); 
    counts{ii} = accumarray(idx, 1); 
end 

現在,這些電池陣列可能包含重複,所以我們會有以在必要時以類似的方式計數結合:以相同的變量名

dates = cell2mat(dates); 
counts = cell2mat(counts); 
[dates, ~, idx] = unique(dates); 
counts = accumarray(idx, counts); % add the counts of duplicated dates together 

注意,重新分配不同的數據像這樣也不是特別好做法 - 我只是感覺非常懶惰,今晚和未來了良好的,描述性的名稱是硬;)