- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Index exceeds matrix dimensions.
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
Feel free to contact me.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Point Cloud Processing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!