MATLAB Answers

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

3,071 views (last 30 days)
jenny orriss on 26 Jan 2017
Commented: the cyclist on 18 Jul 2020
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

the cyclist on 26 Jan 2017
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.
Anila Khan on 19 Nov 2017
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

Guillaume on 26 Jan 2017
Edited: Guillaume on 26 Jan 2017
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.

#### 2 Comments

Diah Junaidi on 27 Nov 2017
How about this sir?? Would you please check mine too. I dont know what going to do sir :(
Ivan Dwi Putra on 10 Mar 2020
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 (3)

the cyclist on 26 Jan 2017
Edited: the cyclist on 26 Jan 2017
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

Diah Junaidi on 27 Nov 2017
How about this sir?? I dont know what should i do :( would you please check mine sir ? :(
the cyclist on 27 Nov 2017
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.

Ivan Dwi Putra on 27 Apr 2020
I need help for my code, i have 3 question
1. if my B matrix 2x4 dimension, the K matrix in LQR is only 1 K or there is two K1 and K2 ?
2. if two K matrix, the K matrix = [K1;K2] or [K1 K2] ?
3. i have error dimension matrix must agree, when i want multiply matrix in state space function
this is my code
function dx = lqrtrain(t,x)
%Parameter Massa
m1 = 80*10^3; % massa train set 1 dalam kg
m2 = 80*10^3; % massa train set 2 dalam kg
g = 10;
%Parameter Gaya
f1 = 117*10^10; % dalam N
f2 = 117*10^10; % dalam N
c_0_2 = 0.01176 ;
c_1_2 = 0.00077616;
c_2_2 = 4.48;
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);
% Initial Conditions
x0 = [0;
0;
500;
0;];
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 ;
];
C = [1 0 0 0;
0 0 1 0;
];
D = 0;
Q = [10 0 0 0;
0 10 0 0;
0 0 10 0;
0 0 0 10;
];
R = [1 1;
1 2
];
K1 = lqr(A,B,Q,R);
K2 = lqr(A,B,Q,R);
K = [-1000.00; -946.79; 4353.27; 803.53;];
K_1 = [10.2
9.9007
211.2757
37.501];
W = [((a_1-1)*v_0)-(p_1*hstar);
0;
((a_2-1)*v_0)-(p_2*hstar);
((a_1-1)*v_0)-(p_1*hstar);
];
sys = ss((A - (B.*K_1)), B, C, D);
% Run response to initial condition
t = 0:0.005:30;
[t,x] = initial(sys, x0, t);
plot(t,x)
end

#### 1 Comment

the cyclist on 27 Apr 2020
You posted this as an "answer" on a 2-year-old question. Almost no one will see it.
Instead, you should post a new question.

Sign in to comment.

krishnasundeep basavaraju on 18 Jul 2020
can any one help me to resolve the error : matrix dimensions must agree
Error in eemd (line 14)
modes = modes + emd(y1,'MaxNumIMF',aim);
the code is given below:
function [modes] = eemd(y, aim, NR, Nstd)
stdy = std(y);
if stdy < 0.01
stdy = 1;
end
y = y ./ stdy;
siz = length(y);
modes = zeros(siz,aim);
for k = 1:NR
disp(['Ensemble number #' num2str(k)]);
wn = (randn(1,siz).*Nstd)';
y1 = y + wn;
y2 = y - wn;
modes = modes + emd(y1,'MaxNumIMF',aim);
if Nstd > 0 && NR > 1
modes = modes + emd(y2,'MaxNumIMF',aim);
end
end
modes = modes .* stdy ./ (NR);
if Nstd > 0 && NR > 1
modes = modes ./ 2;
end
end

#### 1 Comment

the cyclist on 18 Jul 2020
You posted this as an "answer" on a 3-year-old question. Almost no one will see it.
Instead, you should post a new question.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!