通常,當從外部看起來有些棘手問題時,從而不是內部。我們如何才能從單個數組中獲得頂級日期?
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
注意,重新分配不同的數據像這樣也不是特別好做法 - 我只是感覺非常懶惰,今晚和未來了良好的,描述性的名稱是硬;)
謝謝。這工作得很好。但我有個問題。我得到了258天。那是因爲任何零的日子都被遺漏了嗎? – shizishan
是否有可能讓所有的日子,因爲我要畫出他們有差距沿x軸天,將使這更復雜。 – shizishan
@shizishan是的,很可能是因爲我們只選擇第36行。所以,你還是想從第一行36中選擇,但讓所有的日期,只是情節? – Divakar