Index exceeds matrix dimensions.
Show older comments
Index exceeds matrix dimensions.
Error in (line 29) vehicles(i).tasks = [vehicles(i).tasks; t];
the code is
clear;
clc;
close all;
% Parameters
numVehicles = 100; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
cloudProcessingTime = 20; % Time taken by the cloud to process a task (in seconds)
edgeProcessingTime = 10; % Time taken by an edge node to process a task (in seconds)
taskGenerationRate = 0.2; % Task generation rate per vehicle (tasks/second)
cloudDelay = 5; % Communication delay for offloading to cloud (in seconds)
edgeDelay = 2; % Communication delay for offloading to edge (in seconds)
simulationTime = 100; % Simulation time (in seconds)
% Initialize vehicles
vehicles = struct('tasks', []);
% Initialize edge nodes
edges = struct('tasks', []);
% Initialize cloud
cloudTasks = [];
% Simulation loop
for t = 1:simulationTime
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskGenerationRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - edgeDelay - edgeProcessingTime);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge node ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
end
end
% Process tasks at cloud
if ~isempty(cloudTasks)
completedTasks = cloudTasks(cloudTasks <= t - cloudDelay - cloudProcessingTime);
cloudTasks = setdiff(cloudTasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
end
% Offload tasks from vehicles to edge nodes or cloud
for i = 1:numVehicles
if ~isempty(vehicles(i).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
% Decide whether to offload to edge or cloud (simple random choice)
if rand < 0.5
% Offload to edge node
[~, minEdge] = min(arrayfun(@(x) length(x.tasks), edges));
edges(minEdge).tasks = [edges(minEdge).tasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to edge ', num2str(minEdge)]);
else
% Offload to cloud
cloudTasks = [cloudTasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to cloud']);
end
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!