Clear Filters
Clear Filters

Index exceeds matrix dimensions.

3 views (last 30 days)
Catherine
Catherine on 9 Mar 2024
Commented: Hassaan on 10 Mar 2024
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
Index exceeds the number of array elements. Index must not exceed 1.

Accepted Answer

Hassaan
Hassaan on 9 Mar 2024
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 = repmat(struct('tasks', []), numVehicles, 1);
% Initialize edge nodes
edges = repmat(struct('tasks', []), numEdges, 1);
% 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
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!