Matrix dimensions must agree

1 次查看(过去 30 天)
Error using -
Error in VEoTCclusters (line 34)
dist = norm(vehicles(i).position - clusters(j).center);
code
clear;
clc;
close all;
% Parameters
numVehicles = 50; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
numClusters = 5; % Number of clusters in the Things layer
cloudCapacity = 1000; % Cloud computing capacity
edgeCapacity = [100, 150, 200]; % Edge computing capacity
taskRate = 0.1; % Task generation rate per vehicle (tasks/second)
communicationDelayEdge = 1;% Communication delay for edge offloading (seconds)
communicationDelayCloud = 5;% Communication delay for cloud offloading (seconds)
distanceThreshold = 100; % Maximum distance for edge offloading (meters)
clusterRadius = 200; % Radius for clustering in the Things layer (meters)
% Initialize vehicles
vehicles = struct('position', rand(numVehicles, 2)*1000, 'tasks', []);
% Initialize edge nodes
edges = struct('position', rand(numEdges, 2)*1000, 'tasks', [], 'capacity', edgeCapacity);
% Cloud initialization
cloud = struct('tasks', [], 'capacity', cloudCapacity);
% Initialize clusters in the Things layer
clusters = struct('center', rand(numClusters, 2)*1000, 'members', []);
% Form clusters in the Things layer
for i = 1:numVehicles
minDist = inf;
nearestCluster = 0;
for j = 1:numClusters
dist = norm(vehicles(i).position - clusters(j).center);
if dist < minDist
minDist = dist;
nearestCluster = j;
end
end
clusters(nearestCluster).members = [clusters(nearestCluster).members, i];
end
% Simulation loop
for t = 1:1000
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
% Offload tasks from vehicles to either edge nodes or cloud
for i = 1:numVehicles
% Determine if edge offloading is possible
edgeOffload = false;
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
edgeOffload = true;
break;
end
end
% Offload task to edge node if possible, otherwise to cloud
if edgeOffload
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
edges(j).tasks = [edges(j).tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to edge ', num2str(j)]);
break;
end
end
else
if ~isempty(cloud.tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
cloud.tasks = [cloud.tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to cloud']);
end
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - communicationDelayEdge);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
end
end
% Process tasks at the cloud
if ~isempty(cloud.tasks)
completedTasks = cloud.tasks(cloud.tasks <= t - communicationDelayCloud);
cloud.tasks = setdiff(cloud.tasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
end
% Move vehicles
for i = 1:numVehicles
vehicles(i).position = vehicles(i).position + randn(1, 2)*5; % Random movement
end
end

采纳的回答

Voss
Voss 2024-3-15
% Parameters
numVehicles = 50; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
numClusters = 5; % Number of clusters in the Things layer
cloudCapacity = 1000; % Cloud computing capacity
edgeCapacity = [100, 150, 200]; % Edge computing capacity
taskRate = 0.1; % Task generation rate per vehicle (tasks/second)
communicationDelayEdge = 1;% Communication delay for edge offloading (seconds)
communicationDelayCloud = 5;% Communication delay for cloud offloading (seconds)
distanceThreshold = 100; % Maximum distance for edge offloading (meters)
clusterRadius = 200; % Radius for clustering in the Things layer (meters)
You are creating a scalar structure here:
% Initialize vehicles
vehicles = struct('position', rand(numVehicles, 2)*1000, 'tasks', [])
vehicles = struct with fields:
position: [50×2 double] tasks: []
But subsequent code expects that to be non-scalar. (Same for edges and clusters, but not cloud.)
So create a non-scalar structure array instead:
% Initialize vehicles
vehicles = struct('position', num2cell(rand(numVehicles, 2)*1000, 2), 'tasks', [])
vehicles = 50×1 struct array with fields:
position tasks
% check the first one:
vehicles(1)
ans = struct with fields:
position: [47.0680 84.4844] tasks: []
Making those changes, the code runs.
clear;
clc;
close all;
% Parameters
numVehicles = 50; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
numClusters = 5; % Number of clusters in the Things layer
cloudCapacity = 1000; % Cloud computing capacity
edgeCapacity = [100, 150, 200]; % Edge computing capacity
taskRate = 0.1; % Task generation rate per vehicle (tasks/second)
communicationDelayEdge = 1;% Communication delay for edge offloading (seconds)
communicationDelayCloud = 5;% Communication delay for cloud offloading (seconds)
distanceThreshold = 100; % Maximum distance for edge offloading (meters)
clusterRadius = 200; % Radius for clustering in the Things layer (meters)
% Initialize vehicles
vehicles = struct('position', num2cell(rand(numVehicles, 2)*1000, 2), 'tasks', []);
% Initialize edge nodes
edges = struct('position', num2cell(rand(numEdges, 2)*1000, 2), 'tasks', [], 'capacity', edgeCapacity);
% Cloud initialization
cloud = struct('tasks', [], 'capacity', cloudCapacity);
% Initialize clusters in the Things layer
clusters = struct('center', num2cell(rand(numClusters, 2)*1000, 2), 'members', []);
% Form clusters in the Things layer
for i = 1:numVehicles
minDist = inf;
nearestCluster = 0;
for j = 1:numClusters
dist = norm(vehicles(i).position - clusters(j).center);
if dist < minDist
minDist = dist;
nearestCluster = j;
end
end
clusters(nearestCluster).members = [clusters(nearestCluster).members, i];
end
% Simulation loop
for t = 1:1000
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
% Offload tasks from vehicles to either edge nodes or cloud
for i = 1:numVehicles
% Determine if edge offloading is possible
edgeOffload = false;
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
edgeOffload = true;
break;
end
end
% Offload task to edge node if possible, otherwise to cloud
if edgeOffload
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
edges(j).tasks = [edges(j).tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to edge ', num2str(j)]);
break;
end
end
else
if ~isempty(cloud.tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
cloud.tasks = [cloud.tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to cloud']);
end
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - communicationDelayEdge);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
end
end
% Process tasks at the cloud
if ~isempty(cloud.tasks)
completedTasks = cloud.tasks(cloud.tasks <= t - communicationDelayCloud);
cloud.tasks = setdiff(cloud.tasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
end
% Move vehicles
for i = 1:numVehicles
vehicles(i).position = vehicles(i).position + randn(1, 2)*5; % Random movement
end
end
  4 个评论
Catherine
Catherine 2024-3-15
编辑:Catherine 2024-3-15
i go this error and i don't have any paramter called mtimes
Undefined function 'mtimes' for input arguments of type 'cell'.
Error in (line 18)
vehicles = struct('position', num2cell(numVehicles, 2)*1000, 'tasks', []);
clear;
clc;
close all;
% Parameters
numVehicles = 50; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
numClusters = 5; % Number of clusters in the Things layer
cloudCapacity = 1000; % Cloud computing capacity
edgeCapacity = [100, 150, 200]; % Edge computing capacity
taskRate = 0.1; % Task generation rate per vehicle (tasks/second)
communicationDelayEdge = 1;% Communication delay for edge offloading (seconds)
communicationDelayCloud = 5;% Communication delay for cloud offloading (seconds)
distanceThreshold = 100; % Maximum distance for edge offloading (meters)
clusterRadius = 200; % Radius for clustering in the Things layer (meters)
% Initialize vehicles
vehicles = struct('position', num2cell(numVehicles, 2)*1000, 'tasks', []);
% Initialize edge nodes
edges = struct('position', num2cell(numEdges, 2)*1000, 'tasks', [], 'capacity', edgeCapacity);
% Cloud initialization
cloud = struct('tasks', [], 'capacity', cloudCapacity);
% Initialize clusters in the Things layer
clusters = struct('center', num2cell(numClusters, 2)*1000, 'members', []);
% Form clusters in the Things layer
for i = 1:numVehicles
minDist = inf;
nearestCluster = 0;
for j = 1:numClusters
dist = norm(vehicles(i).position - clusters(j).center);
if dist < minDist
minDist = dist;
nearestCluster = j;
end
end
clusters(nearestCluster).members = [clusters(nearestCluster).members, i];
end
% Simulation loop
for t = 1:1000
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
% Offload tasks from vehicles to either edge nodes or cloud
for i = 1:numVehicles
% Determine if edge offloading is possible
edgeOffload = false;
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
edgeOffload = true;
break;
end
end
% Offload task to edge node if possible, otherwise to cloud
if edgeOffload
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
edges(j).tasks = [edges(j).tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to edge ', num2str(j)]);
break;
end
end
else
if ~isempty(cloud.tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
cloud.tasks = [cloud.tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to cloud']);
end
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - communicationDelayEdge);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
end
end
% Process tasks at the cloud
if ~isempty(cloud.tasks)
completedTasks = cloud.tasks(cloud.tasks <= t - communicationDelayCloud);
cloud.tasks = setdiff(cloud.tasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
end
% Move vehicles
for i = 1:numVehicles
vehicles(i).position = vehicles(i).position + randn(1, 2)*5; % Random movement
end
end
Voss
Voss 2024-3-15
编辑:Voss 2024-3-15
Use (this is what I had):
vehicles = struct('position', num2cell(rand(numVehicles, 2)*1000, 2), 'tasks', []);
not
vehicles = struct('position', num2cell(numVehicles, 2)*1000, 'tasks', []);
In othe words, num2cell doesn't replace rand; it converts the random matrix created by rand into a cell array that when passed to struct gives you a non-scalar structure array instead of a scalar structure.
(mtimes is the name of the * (multiplication) operator.)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Satellite Mission Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by