フィルターのクリア

How can I use a while loop to fill in certain cells of an array?

3 ビュー (過去 30 日間)
Brandon Bush
Brandon Bush 2018 年 5 月 22 日
コメント済み: Brandon Bush 2018 年 5 月 22 日
I am currently working with rain gauge data read in from excel. Column 1 has rainfall totals from when the gauge tipped over, column 2 has the year, in this case 2017, column 3 has the month starting with January and ending in October, column 4 has each day of the month when the rain gauge tipped. The 3rd and 4th columns vary with how many times each value is shown, ie) day 1 in January shows in three rows and day 2 shows in 10 rows indicating the number of times the gauge tipped in that day. I need the code to find the last tip of each day, take the rainfall amount for that day and place it in the correct cell of a 365 x 1 array, and do it everyday until the end of the data set.
My idea was to use a while loop since I don't know how many times each value re-occurs.
months = Excel_data(:,3);
days = Excel_data(:,4);
n = 1;
M = end;
d = end;
while months(M) = n;
while days(d) = n;
.....
n = n+1;
end
n = n+1;
end
Any insight would be helpful, Thank you.

回答 (1 件)

Guillaume
Guillaume 2018 年 5 月 22 日
編集済み: Guillaume 2018 年 5 月 22 日
Your data is ideally suited for a table or even a timetable. In fact, if you use a timetable, obtaining what you want is trivial:
newtimetable = retime(yourtimetable, 'daily', 'lastvalue');
Otherwise, assuming your cell array is ordered by day (or at least all values for the same day are grouped together), what you want can be achieved a lot faster than using a loop:
days = Excel_data(:,4);
islastofday = [diff(days) ~= 0; true];
dailydata = Excel_data(islastofday, :);
  1 件のコメント
Brandon Bush
Brandon Bush 2018 年 5 月 22 日
Thank you so much, that worked like a charm.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by