Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Could anyone help me to solve the error in the following code
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Code:
particles=2
distances{1}=[1.8256 1.0045 1.8458;
1.8094 2.4613 2.0386;
0.4744 1.2151 0.7238;
2.0455 1.7725 2.3337;
distances{2}= [1.3906 1.4004 1.4203 1.2257;
2.1105 2.0241 2.0041 2.2337;
0.8330 0.8315 0.8130 0.9896;
1.8031 1.5781 1.5765 1.7164]
swarm_pos{1}=[0.8646 0.5289;
0.0560 0.6944;
0.8169 0.2124]
swarm_pos{2}=[ 0.4416 0.6754;
0.4462 0.9037;
0.4657 0.9085;
0.2790 0.7472]
c=[ 2 4;
1 3;
1 3;
2 3]% taking the index of minimum distance of distances{1} in first column
%and taking the index of minimum distance of distances{2} in second column
for particle=1:particles
PART1=numel(particle);
[mm,nn] = size(swarm_pos{particle})
for cluster = 1 : mm
if any(c(:,particle) == cluster)
CC=c(:,particle)
CCC=c(:,particle)==cluster
CC_CC=c(:,particle)==cluster,cluster,particle
CCCC=distances{particle}(c(:,particle)==cluster,cluster,particle)
CCCCC=mean(CCCC)
local_fitness=mean(distances{particle}(c(:,particle)==cluster,cluster,particle))
end
end
end
When i run the code i am getting error stating index exceeds matrix dimensions.Could anyone please help me to solve it.
0 comentarios
Respuestas (1)
Guillaume
el 10 de Mayo de 2019
There are many problems with your code. I gave up trying to guess which line is causing your error because of these. In any case, if you get an error always give us the full text of the error message so we don't have to guess where the problem is.
The problems:
1) You start with very good variable names, particles, distances, swarm_pos. Then it degenerates into C, CC, CCC. Are you just going to add more Cs as you go along. A meaningful variable name makes it easier to understand what the code is doing.
2)
PART1=numel(particle);
PART1 is never used, so it's unclear what the purpose of this line is. In any case, PART1 will always be 1, since particle is your loop index and thus scalar.
3) No idea what the intent of this line is:
CC_CC=c(:,particle)==cluster,cluster,particle
CC_CC will be exactly identical to the CCC calculated on the previous line. This current line is just 3 statements on the same line and is exactly equivalent to:
CC_CC=c(:,particle)==cluster %so does the same as the previous line. Since it's not terminated by ; also displays the value of CC_CC
cluster %simply displays the value of cluster
particle %simply displays the value of particle
4)
CCCC=distances{particle}(c(:,particle)==cluster,cluster,particle)
So, you've already calculated c(:,particle)==cluster twice, as CCC and CC_CC but you're calculating it again. What was the point of the previous lines? Note that you're doing 3D indexing here but your distances matrices are only 2D. This is probably the cause of the error. No idea what the intent of this was.
I've no idea what the intent of your code is. It's likely that at least one of the loop is not needed. So, please explain what you're trying to do and we can show you the proper way of writing it.
2 comentarios
Guillaume
el 10 de Mayo de 2019
Again, the error is because you do 3D indexing on 2D matrices. I have no idea why you're trying to 3D index a 2D matrix and you've still not explained what you're trying to do. So what is the intent of the loop? What are you trying to calculate
I understood your input data. However, a puzzling thing is that c has four rows for particle 2 and 1 but distance and position has only 3 rows for particle 1. And actually, that will also cause errors in your loop. Shouldn't c be another cell array as well?
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!