2017-01-19 4 views
0

他の列が条件付きステートメントでチェックされている場合に、列の値を合計するマトリクスがあります。方法。条件文に応じて列の部分を合わせるMatlab

レイアウト入力テーブルListMax:

year Month day hour precipitation 
1998 1  1  1  5 
1998 1  2  2  7 
1998 1  3  3  0 
.... ...  ... ...  ... 

レイアウト出力result_matrix:

year jan feb mar  
1998 100 120 140 
1999 90  110 130 
... ...  ... ... 

私は、行を超えるitiratingの組み合わせでstruggeling条件文をチェックし、保存しています出力の値。

私は次の点に注意していたが、それは働いていない:私は今、これはおそらく最も簡単な方法はありませんが、現時点では、この私は考えることができるのアプローチを見ることができます

result_matrix = zeros(15,12); %empty matrix 15 years, 12 months 
year_number = 1998; 
counting_years = 1; 
counting_months = 1; 

for (ii = 1: length(ListMax)); 
    if ListMax(:,1) == year_number && ListMax(:,2) == counting_months; 
     result_matrix(counting_years, counting_months) = (sum(Listmax(:,5)); 
    else % update month itirators 
     if counting_months < 12 
      counting_months = counting_months + 1; 
     else % end of year, set month count to 1 
      counting_months = 1; 
     end 
    year_number = year_number +1; 
    counting_years = counting_years + 1; 
    end 
end 

私はちょうどそれを稼働させる必要があります。右方向への任意のプッシュもいただければ幸いです:)

答えて

0

この(未テスト)について、どのように:

year_start = 1998; 
year_end = 2016; 
result_matrix = zeros(year_end-year_start+1,12); 

for year = year_start:year_end 
    for month = 1:12 
     rows = (ListMax(:,1)==year) & (ListMax(:,2)==month); 
     result_matrix(year-year_start+1,month) = sum(ListMax(find(rows),5)); 
    end 
end 
+0

これは本当に素晴らしい仕事は、あなたに感謝します! – Jobbo90

1

あなたは、単にインデックスをサブ2つのカラムを持つaccumarrayを使用することができます。例えば

year Month day hour precipitation 
1998 1  1  1  5 
1998 1  2  2  7 
1998 1  3  3  0 
.... ...  ... ...  ... 

あなたは年にと月による降水量の合計量を合計したい場合、あなたはインデックスと降水量の値として、最後の一人として、あなたのマトリックスの2つの最初の列を使用することができます。

sub = 1998 1  %Jan.1998 
     1998 1  %Jan.1998 
     1998 2  %Feb.1998 
     ... 
sub(:,1) = sub(:,1) - (min(sub(:,1))-1); %so the index for the year start at 1 and not 1998. 

val = 6 %millimeter of precipitation on Jan.1998 day xxx 
     4 %millimeter of precipitation on Jan.1998 day xxx 
     7 %millimeter of precipitation on Feb.1998 day xxx 
     .... 

そして今accumarrayを使用します。

​​
+0

あなたのアイデアをありがとう、まだそれをテストしていないが、私はアプローチが好きです! :) – Jobbo90

関連する問題