Could anyone help me to solve the issue.

1 次查看(过去 30 天)
code:The below code executes and gives the result.
But i want to view swarm_pos for all particles in workspace.
When I run the code it displays only with respect to the second time of for loop result in workspace.
could anyone please help me on this.
particles=2;
dimensions=2;
PPP=[1,2;
3,4;
5,6;
7,8];
centroids=zeros(1,particles);
for particle=1:particles
P=numel(particle);
users=size(PPP,1);
centroids(particle)= randi(users,1,P)
fprintf('\nCentroids=%d\n',centroids(particle))
swarm_pos =rand(centroids(particle),dimensions,P)
end

采纳的回答

Bhaskar R
Bhaskar R 2019-5-8
编辑:Bhaskar R 2019-5-8
Since the matrix size assigning to the variable "swarm_pos" is deifferent for each iteration, initialize the matrix as cell array class matrix, then you can get the variable as you required in the workspace
Modify your code as below
particles=2;
dimensions=2;
PPP=[1,2;
3,4;
5,6;
7,8];
centroids=zeros(1,particles);
% Initialize your variable as cell array type as your required dimention either (1xparticles) or (particlesx1)
swarm_pos = cell(1,particles);
for particle=1:particles
P=numel(particle);
users=size(PPP,1);
centroids(particle)= randi(users,1,P)
fprintf('\nCentroids=%d\n',centroids(particle))
swarm_pos{particle} =rand(centroids(particle),dimensions,P) % stores array values for each iteration
end
Access you data as
swarm_pos{1}, swarm_pos{2}
  2 个评论
jaah navi
jaah navi 2019-5-9
ok.It works
I then continued the code as follows:
particles=2;
dimensions=2;
PPP=[1,2;
3,4;
5,6;
7,8];
centroids=zeros(1,particles);
swarm_pos = cell(1,particles);
for particle=1:particles
P=numel(particle);
users=size(PPP,1);
centroids(particle)= randi(users,1,P)
fprintf('\nCentroids=%d\n',centroids(particle))
swarm_pos{particle} =rand(centroids(particle),dimensions,P) % stores array values for each iteration
end
for T=1:iterations
for particle=1:particles
P=numel(particle);
distances=zeros(users,centroids(particle),P)
for centroid=1:centroids(particle)
distance=zeros(users(particle),1);
for user=1:users
distance(user,1)=norm(swarm_pos(centroid,:,P)-PPP(user,:))
end
distances(:,centroid,P)=distance
end
end
end
wheni run the code i am getting error stating Undefined operator '-' for input arguments of type 'cell'.
Could you please help me to overcome it.
Bhaskar R
Bhaskar R 2019-5-9
As I don't know your requiremet i couldn't get how are you doing norm at error occured.
Any way I can suggest you to access values from the the variable "swarm_pos" you can make change as
distance(user,1)=norm(swarm_pos{particle}(centroid,:,P)-PPP(user,:));
Even this modification is not enough for you, you get error in the statement
distance=zeros(users(particle),1);
because users is the scaler variable
I need these point to rectify error

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by