运行上述程序显示位置 2 处的索引超出数组边界(不能超出 4)。
时间: 2023-09-21 13:07:51 浏览: 58
非常抱歉,这是我的失误。下面是修改后的代码:
```matlab
% initialize data
targets = 4;
sensors = 11;
start_times = [20, 100, 400, 60; % start time for each target
100, 300, NaN, NaN;
NaN, NaN, NaN, 400;
NaN, NaN, NaN, NaN];
end_times = [800, 1500, 800, 600; % end time for each target
500, 1500, NaN, NaN;
NaN, NaN, NaN, 1500;
NaN, NaN, NaN, NaN];
sensor_assignment = zeros(targets, sensors); % sensor assignment matrix
colors = ['r', 'g', 'b', 'm']; % color for each target
figure;
hold on;
% iterate over targets
for t = 1:targets
% iterate over assigned sensors
for s = 1:sensors
start_time = start_times(t, s);
end_time = end_times(t, s);
if ~isnan(start_time) && ~isnan(end_time)
% assign sensor to target
sensor_assignment(t, s) = 1;
% plot sensor task on Gantt chart
plot([start_time, end_time], [t, t], colors(t), 'LineWidth', 20);
end
end
end
% add legend and axis labels
legend('Target 1', 'Target 2', 'Target 3', 'Target 4', 'Location', 'NorthEastOutside');
xlabel('Time (s)');
ylabel('Target Number');
% set axis limits and tick marks
xlim([0, 1400]);
ylim([0.5, targets + 0.5]);
set(gca, 'YTick', 1:targets, 'YTickLabel', {'1', '2', '3', '4'});
```
请您再次尝试运行。如果还有问题,请随时告诉我。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)