Index in position 2 exceeds array bounds. Index must not exceed 27.

1 回表示 (過去 30 日間)
john stapke
john stapke 2022 年 1 月 19 日
コメント済み: Walter Roberson 2022 年 1 月 19 日
Problem: I recieve this error message:
Entire Error Message:
Index in position 2 exceeds array bounds. Index must not exceed 27.
Error in find_precip_JS (line 46)
p_now = hourly(ind, time_ind); % rainfall now
Error in find_dbTP_Valley_Rim_JS (line 56)
YYV_RF_Precip = find_precip_JS(date, unit_type, YYV_PRECIP_JS);
I can't figure out why I am recieving an error message "Index in position 2 exceeds array bounds. Index must not exceed 27." when none of my INPUT matrices exceed 27 in position 2...
FINAL CODE:
FINAL_OUTPUT = find_dbTP_Valley_Rim_JS(RFDB, unit_type, GIN_TEMPS_JS_STUCT, YYV_TEMPS_JS_STRUCT, YYV_PRECIP_JS)
where
INPUTS:
%RFDB = 1485 x 5 double grid
unit_type = 'english'
%GIN_TEMPS_JS_STUCT = 1 x 1 structural array with GIN_TEMPS_JS_STUCT.hourly double array [13047 x 27]
%YYV_TEMPS_JS_STRUCT = 1 x 1 structural array with GIN_TEMPS_JS_STUCT.hourly double array [80363 x 27]
%YYV_PRECIP_JS = 8046 x 27 double array
elev_rim = 2377; % 7800';
elev_valley = 1280; % 4200';
S = size(RFDB);
L = S(1);
for i = 1:L
cur = RFDB(i, :);
year = cur(2);
month = cur(3);
day = cur(4);
time = cur(5);
if day == 0
% change the # of -100 depending on find_temp output length
RF_RIM_TEMPS = [-100, -100, -100, -100, 0];
RF_YYV_TEMPS = [-100, -100, -100, -100, 0];
RF_YYV_PRECIPS = [-100, -100, -100, -100];
RFDB(i, :) = [cur, RF_RIM_TEMPS, RF_YYV_TEMPS, RF_YYV_PRECIPS];
continue
elseif time < 0
time = 23/24;
end
date = datenum(year, month, day) + time;
datestr(date);
RF_RIM_TEMPS = find_temp_JS(date, unit_type, elev_rim, GIN_TEMPS_JS_STUCT);
RF_YYV_TEMPS = find_temp_JS(date, unit_type, elev_valley, YYV_TEMPS_JS_STRUCT);
YYV_RF_Precip = find_precip_JS(date, unit_type, YYV_PRECIP_JS);
JS_TP(i, :) = [cur, RF_RIM_TEMPS, RF_YYV_TEMPS, YYV_RF_Precip];
% Final OUTPUT^^
Note: Two codes shown below find_precip and find_temp are used within the final code "find_dbTP_Valley_Rim"
Find_Precip Code Used in FINAL Code Above
function[out_data] = find_precip(date, unit_type, YYV_PRECIP_JS)
%% The objective is to find the precipitation of at the time of the rockfall
%
% out_data = find_precip(date, unit_type, precip_data)
%
% Inputs:
% precip data = [year, month, day, P_12am, P_1am, ..., P_11pm]
% unit_type = 'metric' for precip in centimeters
%
% out_data = [yearly cumulative, last 30 days, last 72 hours, last 24 hours]
%
%
% Requirements: Precip DATA FILE ASSUMED TO BE WELL-BEHAVED! No erroneous
% points
%
% ** Also, past 24 and past 72 hours may give bad data if the rock fall
% occurs on July 1, 2, or 3 AND it was raining (Data is set to zero).
%
%% Collect the data for the appropriate day
day = floor(date);
time = round((date-day)*24);
time_ind = 4 + time;
hourly = YYV_PRECIP_JS;
pyear = hourly(:, 1);
pmonth = hourly(:, 2);
pday = hourly(:, 3);
p_days = datenum(pyear, pmonth, pday);
ind = find(p_days == day);
if isempty(ind)
out_data = [-100, -100, -100, -100];
return
end
p_now = hourly(ind, time_ind); % rainfall now
p_yest = hourly(ind-1, time_ind); % rain at this time yesterday
p_24 = p_now - p_yest; % 24 hour rainfall
p_minus3 = hourly(ind-3, time_ind); % rain at this time 72 hrs ago
p_72 = p_now - p_minus3; % 72 hour rainfall
% sets data to zero if negative (will occur with good data on July 1
% (2, 3)), but this is wrong IF it was raining
if p_24 < 0
p_24 = 0;
end
if p_72 < 0
p_72 = 0;
end
dv = datevec(date);
%ind
if dv(2) == 7 %dv(2) is the month of the rf
% Code assumes a very well-behaved data file
dom = dv(3); % day of month
July1_ind = ind-dom+1;
%datestr(p_days(July1_ind))
pm1 = hourly(July1_ind, 4); %rainfall, july start -> should be ZERO!
%datestr(p_days(July1_ind-1))
pm2 = hourly(July1_ind-1, 4); %rainfall, june30
%datestr(p_days(ind-30))
pm3 = hourly(ind-30, 4);
p30a = p_now - pm1;
p30b = pm2 - pm3;
p_30 = p30a + p30b;
else
p_month = hourly(ind - 30, 4);
p_30 = p_now - p_month;
end
out_data = [p_now, p_30, p_72, p_24];
% convert to centimeters?
if strcmpi(unit_type,'metric')
out_data = out_data * 2.54;
end
Find_Temp Code Used in Final Code Above
function[out_data] = find_temp(date, unit_type, elev, YYV_TEMPS_JS_STRUCT)
% out_data = find_temp(date, unit_type, elev, temperature from YYV_TEMPS_JS_STRUCT)
% Inputs:
% - date = matlab datenum
% - elevation = elevation, in meters, for which you desire temperature
% corrections
% - unit_type = 'metric' for celcius units 'english' for fahrenheit
% - YYV_TEMPS_JS_STRUCT or GIN_TEMPS_JS_STUCT= structure of temperature data, including:
% name = 'Weather Station Name'
% hourly = [year, month, day, T_12am, T_1am, ..., T_11pm]
% daily = [datenum, max, min, avg]
% elevation (in meters)
% days = list of datenums of all days in db
%
% out_data = [min, avg, max, temp at rock fall, # stations contributing data]
%% Constants
t_grad = 0.0;
%% Collect the data for the appropriate day
day = floor(date);
time = round((date-day)*24);
time_ind = 4 + time;
hour = [time+1:1:23, 0:1:time];
L = length(YYV_TEMPS_JS_STRUCT or GIN_TEMPS_JS_STRUCT); %input one of these
cur = [];
k = 1;
for i = 1:L
t_all = [];
hourly = YYV_TEMPS_JS_STRUCT(i).hourly or GIN_TEMPS_JS_STRUCT; %input one of these
t_days = datenum(hourly(:, 1), hourly(:, 2), hourly(:, 3));
ind = find(t_days == floor(date));
% Create array of temps in the 24 hours prior to the rf (previous
% hi/lo)
if time_ind < 27
t_today = hourly(ind, 4:time_ind);
t_yesterday = hourly(ind-1, time_ind+1:27);
t_all = [t_yesterday t_today];
%t_h = [time-23:1:time];
else
%t_h = [0:1:23];
t_all = hourly(ind, 4:27);
end
%t_all
ind2 = t_all > -30;
t_all = t_all(ind2);
ind2 = t_all < 120;
t_all = t_all(ind2);
if isempty(t_all)
continue
% Do Nothing
else
%t_all
YYV_TEMPS_JS_STRUCT(i).name;
cur(k).t_max = max(t_all);
cur(k).t_min = min(t_all);
cur(k).t_avg = mean(t_all);
cur(k).elevation = YYV_TEMPS_JS_STRUCT(i).elevation;
end
%% Find the temperature nearest to the time of the rock fall
if time == 23 % this is a fake time, not appropriate for the rock fall
time = 21; % 7pm is a neutral time
time_ind = 4 + time;
end
% Create an array of the X hours surrounding the rock fall
plusminus = 6; % plusminus hours before and plusminus hours after
%time_ind
if time_ind > 27 - plusminus % late in the day
t_today = hourly(ind, time_ind-plusminus:27);
offset = plusminus - (27 - time_ind) - 1;
t_tomorw = hourly(ind+1, 4:4+offset);
t_near = [t_today t_tomorw];
elseif time_ind < 4 + plusminus % early in the day
t_today = hourly(ind, 4:time_ind+plusminus);
offset = plusminus - (time_ind - 4) - 1;
t_yesrdy = hourly(ind-1, 27-offset:27);
t_near = [t_yesrdy t_today];
else
t_near = hourly(ind, time_ind-plusminus:time_ind+plusminus);
end
t_h = [plusminus*-1:1:plusminus];
ind3 = t_near > -30;
t_near = t_near(ind3);
t_h = t_h(ind3);
ind3 = t_near < 120;
t_near = t_near(ind3);
t_h = t_h(ind3);
n_ind = find(t_h == 0);
% No data for time of rock fall
if isempty(t_near)
cur(k).t_rf = -100; % no data within plusminus hours of the rock fall
% Exact data not available, but before and after data are available
elseif isempty(n_ind)
b_ind = find(t_h < 0, 1, 'last');
a_ind = find(t_h > 0, 1, 'first');
% No before data: check to see if after data is close
close = 4; % Close is defined as less than "close" hours
if isempty(b_ind)
if t_h(a_ind) < close;
cur(k).t_rf = t_near(a_ind);
else
cur(k).t_rf = -100;
end
% No after data: check to see if before data is close
elseif isempty(a_ind)
if t_h(b_ind) > -1 * close
cur(k).t_rf = t_near(b_ind);
else
cur(k).t_rf = -100;
end
% Both before and after data: linear extrapolation
else
span = t_h(a_ind) - t_h(b_ind); % hours between before and after
temp_span = t_near(a_ind) - t_near(b_ind);
hr_incre = temp_span/span;
t_rf_tmp = t_near(b_ind) + (hr_incre * (t_h(b_ind)*-1));
cur(k).t_rf = t_rf_tmp;
end
else
cur(k).t_rf = t_near(n_ind); % exact data available, use it!
end
k = k + 1;
end
%% Evaluate the temperature at the appropriate elevation
if isempty(cur)
out_data = [-100, -100, -100, -100 0];
return
else
num_stat = length(cur);
end
%% It is possible to have min/max/avg data BUT NO temp at time of rock fall data
m = 1;
t_rf = -100;
for j = 1:num_stat
t_elev = cur(j).elevation;
t_diff = ((t_elev - elev)/100) * t_grad;
t_max(j) = (cur(j).t_max + t_diff);
t_min(j) = (cur(j).t_min + t_diff);
t_avg(j) = (cur(j).t_avg + t_diff);
t_rf_tmp = cur(j).t_rf;
if t_rf_tmp == -100
% Do nothing
else
t_rf(m) = (t_rf_tmp + t_diff);
m = m + 1;
end
end
  2 件のコメント
Image Analyst
Image Analyst 2022 年 1 月 19 日
Highlight your code and click the Code icon so people can easily copy it into the clipboard.
Walter Roberson
Walter Roberson 2022 年 1 月 19 日
You do not give us the code for find_precip_JS -- only for find_precip

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

回答 (1 件)

KSSV
KSSV 2022 年 1 月 19 日
Error is clear and simple. You are trying to extract more number of elements than present in the array.
A = rand(1,27) ;
A(1) % no error
ans = 0.2747
A(10) % no error
ans = 0.3553
A(27) % no error
ans = 0.5465
A(28) % error as there is no 27th element in A
Index exceeds the number of array elements. Index must not exceed 27.
Check the dimensions of the variable. Learn debugging.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by