problem in matlab code

Hello,
Please how i can solve this problem in my Matlab code:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in ObjectiveFunction (line 73)
throughputCR(i)= throughputCR(i)+W*log2(1+ SinrCR(i,j));
Error in PSO (line 94)
Swarm.Particles(k).O = fobj(currentX);
Error in main (line 54)
[ GBEST , cgcurve ] = PSO( noPa , maxItera , visFlaga ) ;
>>

Answers (2)

W is probably a vector and so the entire right hand side is a vector. You can't stuff, say, 100 elements into throughputCR(i) which is just a single element.
If you put these lines before the bad line, what does it report in the command window?
whos throughputCR
whos W
whos SinrCR

3 Comments

hello, i still have this problem in my matlab code:
26.9763 0 0 0 0
12.6823 0 0 0 0
g =
1
Generation #1
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in GeneticAlgorithm (line 11)
cgcurve(g) = population.Chromosomes(indx(1)).fitness;
Error in Main (line 36)
[BestChrom] = GeneticAlgorithm (M , N, MaxGen , Pc, Pm , Er , Problem.obj , visualization)
>>
The problem is still the same, population.Chromosomes(indx(1)).fitness is a vector, a matrix, or is empty. I.E: it's not just one element. And you try to assign it to a single element of a variable.
brahmi, why did you not put in the whos statements like I specifically asked you for? You're just delaying an answer by not doing the things we suggest.

Sign in to comment.

A= B;

7 Comments

i dont have marameters denoted A and B
Then What does this line means?
Inn an assignment A(:) = B,
Share complete code.
"In an assignment A(:) = B, the number of elements in A and B must be the same" is the generic error message you get (in some versions of matlab) when you attempt an indexed assigment with mismatched size. The error message does not replace A and B by the names of the offending variables.
how can i solve this problem
when i place C=1; V=1; by a values greator than 1, the code give this error:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in ObjectiveFunction (line 73)
throughputCR(i)= throughputCR(i)+W*log2(1+ SinrCR(i,j));
Error in PSO (line 94)
Swarm.Particles(k).O = fobj(currentX);
Error in main (line 54)
[ GBEST , cgcurve ] = PSO( noPa , maxItera , visFlaga ) ;
and the code don't gennerate que generation 1
This is the code of indx(1):
g = 1
disp(['Generation #' , num2str(g)]);
[max_val , indx] = sort([ population.Chromosomes(:).fitness ] , 'descend');
cgcurve(g) = population.Chromosomes(indx(1)).fitness;
Show us the code for ObjectiveFunction
this is the code of the objectivefunctioon
% *************************************************************************************************************************************************
function [fitness_value] = Sphere( X )
% *************************************************************************************************************************************************
global C;
global V;
global RB;
global Noise
global W;
global Pcellular;
global binv;
global Pv;
global PositionsBS;
global PositionsC;
global PositionsV;
constraintSatified=true;
Pcellular=23*X(:,:,1);
Pv=X(:,:,1);
%binv=X(:,:,1);
binv=zeros(V,RB);;
global binc;
binc=zeros(C, RB);
for i=1:C
for j=1:RB
if i==j
binc(i,j)=1;
end
end
end
%matrix user in line, column RB
SINRcth=0;
SINRvth=0;
%calculate the throughput and the interferences of a eh user type
throughputC=zeros(C);
SinrC=zeros(C,RB);
for i=1:C
Icv=0;
for j=1:RB
if binc(i,j)==1
for v=1:V
if binv(v,j)==1
Icv=Icv+Pv(v,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsV(v,1),PositionsV(v,2) );
end
end
SinrC(i,j)=Pcellular(i,j)*calculate_gain(PositionsBS(1),PositionsBS(2), PositionsC(i,1),PositionsC(i,2) )/(Icv+Noise);
throughputC(i)= throughputC(i)+W*log2(1+ SinrC(i,j));
plot( throughputC(i));
end
end
if throughputC(i)<SINRcth
constraintSatified=false;
end
end
throughputV=zeros(V);
SinrV=zeros(V,RB);
for v=1:V
Ivc=0;
for j=1:RB
if binv(i,j)==1
for c=1:C
if binc(c,j)==1
Ivc=Ivc+Pcellular(i,j)*calculate_gain(PositionsC(i,1),PositionsC(i,2), PositionsV(v,1),PositionsV(v,2) );
end
end
SinrV(v,j)=Pv(v,j)*calculate_gain(PositionsV(v,1),PositionsV(v,2),PositionsC(c,1),PositionsC(c,2) )/(Ivc+Noise);
throughputV(v)= throughputV(v)+W*log2(1+ SinrV(v,j));
disp( throughputV(v));
end
end
if throughputV(v)< SINRvth
constraintSatified=false;
end
end
if constraintSatified==false
fitness_value = 0;
else
a=sum(throughputV);
b=sum(throughputC);
fitness_value=max(a,b);
disp(fitness_value);
end
end
global W;
You do not show us where you initialized W. W might be its default value for a global variable, which is to say it might be empty.

Sign in to comment.

Tags

Asked:

on 4 Aug 2019

Commented:

on 6 Aug 2019

Community Treasure Hunt

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

Start Hunting!