How can I fix this error - "Error using / Matrix dimensions must agree"

I am trying to get values for y with T in the range 800 - 1100. the function I have called antoine works for single values but i get the error seen in the screen shot when i try to use the linspace command. Any idea how i can fix this?

2 Comments

For future reference, it is better to upload the code itself, rather than a screenshot of the code. That way, we can paste it into MATLAB directly.
When i run this program it give me error. Error screen shoot attach here please guide me.
clc clear all close all m=30; n=30; snr=20; %Generation of corelation matrix
R=eye(m,m) R(m+1:m+1:end)=0.2; R(2:m+1:end)=0.2;
generations=100:100:500 no_ofDataRuns=500 for gen=1:length(generations) genError=0 for i=1:no_ofDataRuns B=randsrc(m,n) tx=randsrc(1,m) r=awgn(tx,snr) FF=Fit_PSO(r, B, R, m) for cyc=1:generations(gen) Pb=FF(:,2:end) %pb=B; Gb=FF(1,2:end) V=zeros(m,n) for k=1:m C=(Pb(k,:) - B(k,:)); D=(Gb - B(k,:)); V(k,:)=V(k,:) + rand().*C + rand().*D; %V(k,:)=V(k,:) + rand().*(Pb(k,:) - B(k,:)) + rand().*(Gb - B(k,:));
end
for a=1:m
for b=1:n
S(a,b)=1/(1+exp(-V(a,b)))
if S(a,b)>rand()
B(a,b)=1
else
B(a,b)=-1
end
end
end
updated_fitness=Fit_PSO(r,B,R,m)
updated_fitness_error=updated_fitness(1,2:end)
Error(cyc)=pdist([updated_fitness_error;tx],'hamming')
end
genError=(genError+Error(cyc))
end
BER(gen)=genError/[generations(gen)*no_ofDataRuns]
end semilogy(generations,smooth(BER),'Color',[0 0 1]) xlabel('Number of cycles'); ylabel('Bit Error Rate'); txt = strcat('BER Vs generations with ',int2str(snr),'db AWGN') title('BER Vs NOC') grid on; hold on;

Sign in to comment.

 Accepted Answer

Learn the different between arrays (eg ./) and matrix (eg /) operators.
Replace the two / by ./, and ^2 by .^2
And if you want to avoid potential nasty surprises in future code, never write a subtraction as
a -b
either put a space on both sides of the operator
a - b
or on neither:
a-b
Compare the result of
[2 - 1]
[2-1]
[2 -1]
to see why your notation is problematic.

3 Comments

How about this sir?? Would you please check mine too. I dont know what going to do sir :(
function dx = trainfinalmodel(t,x)
%Parameter Massa
m1 = 65; % massa train set 1 dalam kg
m2 = 65; % massa train set 2 dalam kg
g = 10;
%Parameter Gaya
f1 = 117; % dalam N
f2 = 117; % dalam N
c_0_1 = 0.0016*m1*g;
c_1_1 = 0.00008*m1*g;
c_2_1 = 0.0000006*m1*g*(x(2)^2) ;
c_0_2 = 0.0016*m2*g;
c_1_2 = 0.00008*m2*g;
c_2_2 = 0.0000006*m2*g*(x(4)^2) ;
v_0 = 60;
hstar = 120;
a_1 = -1/m1*(c_1_1+2*c_2_1*v_0);
a_2 = -1/m2*(c_1_2+2*c_2_2*v_0);
a_1_head = 1-(a_1*hstar);
a_2_head = 1-(a_2*hstar);
b = 1;
p_1 = -1/m1*(c_0_1 - c_2_1*(v_0)^2);
p_2 = -1/m2*(c_0_2 - c_2_2*(v_0)^2);
x = [x(1);x(2);x(3);x(4)];
x_transpost_t = transpose(x);
A = [0 a_1_head 0 0;
0 0 0 0;
0 (a_2_head-1) 0 a_2_head;
0 0 0 0;
];
B = [-b*hstar 0;
b 0;
0 -b*hstar;
-b -b;
];
U_t = [f1;f2];
W = [((a_1 - 1)*v_0) - p_1*hstar;
((a_2 - 1)*v_0) - p_2*hstar;
];
dx = (A.*x_transpost_t) + (B.*U_t) + W;
end
please check my mine too, i have same problem the matrix dimension must agree

Sign in to comment.

More Answers (1)

You need to use element-wise division, not matrix division.
Use "./" in place of "/"
See this documentation for an explanation of the difference.
You are similarly going to want ".^" rather than just "^" for raising to a power.

2 Comments

How about this sir?? I dont know what should i do :( would you please check mine sir ? :(
Rather than putting a comment a question that is nearly a year old, I suggest that you create a new question. Attach your code (as you did here), tell us the full text of any error messages you are getting, and ask specific questions.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!