data from one array to another

조회 수: 2 (최근 30일)
kamran waheed
kamran waheed 2021년 3월 27일
댓글: the cyclist 2021년 3월 27일
I am trying to put the coresponding value from a set of data from the max value from each column into a select array based on the row the max value occured in and i'm not sure what i've done wrong.
particle.clustervalues is a 15x100 array, x is 100x2 array (data) and particle(i).cluster is 15 arrays where if the max value in column 1 clustervalues occurs on row 9, row x(1) goes into array 9. Any help would be appreciated.
  댓글 수: 2
kamran waheed
kamran waheed 2021년 3월 27일
what i got so far
clc;
clear;
close all;
basestation = 100*rand(1,2);
x = [rand(100,2)*100;50 50];
ResEn = 20*rand(100,1);
k = randperm(100);
emptyparticle.position = [];
emptyparticle.velocity = [];
emptyparticle.energy = [];
emptyparticle.cluster = cell(15,1);
emptyparticle.cost = [];
emptyparticle.best.position = [];
emptyparticle.best.cost = [];
emptyparticle.clustervalues = [];
particle = repmat(emptyparticle,20,1);
for i = 1:20
k = randperm(100);
emptyparticle.position.cluster = x(k(1:15),:);
particle(i).position = x(k(1:15),:);
particle(i).velocity = 0;
particle(i).energy = ResEn(k(1:15),:);
end
for i = 1:20
for k = 1:15
for j = 1:100
if pdist2(particle(i).position(k),x(j))>50
particle(i).clustervalues(k,j) = 0;
elseif pdist2(particle(i).position(k),x(j))<50
if size(particle(i).cluster{k},1) == 0
particle(i).clustervalues(k,j) = 1/(pdist2(particle(i).position(k),x(j)*pdist2(x(101),particle(i).position(k))));
%%[m, ind] = max(particle(i).clustervalues(j));
%%particle(i).cluster{ind} = [x(j,:)]
m(j) = max(particle(i).clustervalues(j));
particle(i).cluster{find(particle(i).clustervalues(j)==m(j))} = [particle(i).cluster{find(particle(i).clustervalues(j)==m(j))};x(j,:)];
elseif size(particle(i).cluster{k},1) > 0
particle(i).clustervalues(k,j) = 1/(pdist2(particle(i).position(k),x(j)*pdist2(x(101),particle(i).position(k))*size(particle(i).cluster{k},1)));
m(j) = max(particle(i).clustervalues(j));
particle(i).cluster{find(particle(i).clustervalues(j)==m(j))} = [particle(i).cluster{find(particle(i).clustervalues(j)==m(j))};x(j,:)];
%%[m, ind] = max(particle(i).clustervalues(j));
%%particle(i).cluster{ind} = [particle(i).cluster{ind};x(j,:)];
end
end
end
end
end
the cyclist
the cyclist 2021년 3월 27일
I am finding it impossible to help. Here are the reasons why:
  • Your code is completely uncommented. It is not possible for me to follow the logic of what each line is supposed to be doing.
  • I don't really understand the problem. When you say, "put the coresponding value from a set of data from the max value from each column", it is very unclear to me what you mean. What data? What column?
  • The way you have structured your data is quite complex. Maybe this is necessary, but it is still difficult to understand.
  • The problem is large enough that it is not easy to focus on a small piece, to see what is wrong.
I would recommend the following, to get help:
  • Comment everything in the code, so that another user can understand what is going on. (This will also help you 6 months from now, when you have forgotten everything going on. Trust me.)
  • Every place that you currently have "100", change that to a parameter, with a descriptive name. (Maybe it is something like NPARTICLES = 100;)
  • Do the same thing for all other constant parameters
  • For code testing purposes, make all of those parameters the smallest value the makes sense. For example, instead of 100, maybe it can be 3.
  • Because you are using random number, use rng default at the beginning of your code (for testing only), so that you get the same random numbers each time.
  • Post your problem again (not just as a comment here), so that more people see it again. Feel free to tag me with @the cyclist, and I'll try to take a look again.
Doing all of this may also help you discover the error on your own, without our help. It is all simply good coding practice.
I hope that helps.

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

답변 (1개)

the cyclist
the cyclist 2021년 3월 27일
I can't tell what you intend to get from this expression
[x(j),:]
which is not valid MATLAB syntax. x(j) is the j-th element of x, and the square brackets would be used to concatenate two expressions. But concatenating to the colon doesn't make sense.
The colon can mean "all elements along this dimension" ... but you are not indexing into a variable.
So, I'm confused about what you are trying to do.
  댓글 수: 1
kamran waheed
kamran waheed 2021년 3월 27일
my bad i corrected this mistake but am still confused. when i run this code it gives me arrays of random lengths

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by