Index exceeds matrix dimensions.

조회 수: 2 (최근 30일)
Catherine
Catherine 2024년 3월 9일
댓글: Hassaan 2024년 3월 10일
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.

채택된 답변

Hassaan
Hassaan 2024년 3월 9일
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.
  댓글 수: 2
Catherine
Catherine 2024년 3월 9일
thank you so much
Hassaan
Hassaan 2024년 3월 10일
You are welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Point Cloud Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by