MATLAB Answers

Error using / Matrix dimensions must agree...

2,015 views (last 30 days)
Austin
Austin on 2 Oct 2013
Commented: Image Analyst on 27 Apr 2020
Below is my script:
>> clf
>> hold on
>> axis([-1 1 -1 1])
>> x=-1:0.0001:1;
>> y=cos(1/x)
>> plot(x,y)
May I know why I keep getting this error:-
(Error using / Matrix dimensions must agree...)?

  1 Comment

guadalupe presa
guadalupe presa on 22 Sep 2016
kEST=(s/(n+g+d))^(1/(1-alfa)); % realiza un código (k*)=steady state (NIVEL DE CAPITAL DE EQUILIBRIO)
SteadyState=findobj('Tag','STEADYSTATE'); set(SteadyState,'String',kEST);
upper=ceil(kEST)*2; jump=upper/10; k=(0:jump:upper)';
ec1=(n+g+d).*k; %ecuación 1= capital consumido: (n+g+d)*k (RECTA - FUNCION DE PRODUCCION) ec2=s.*k.^alfa; %ecuación 2= de capital acumulados:s*k^alfa (CURVA - FUNCION DE INVERSIÓN)
error: Error using / Matrix dimensions must agree.
Error in MODELOSOLOWCURVASFASE (line 87) kEST=(s/(n+g+d))^(1/(1-alfa)); % realiza un código (k*)=steady state (NIVEL DE CAPITAL DE EQUILIBRIO)

Sign in to comment.

Accepted Answer

Amit Nambiar
Amit Nambiar on 2 Oct 2013
It should be like this: >> clf >> hold on >> axis([-1 1 -1 1]) >> x=-1:0.0001:1; >> y=cos(1./x) >> plot(x,y)
always use .* ./ (mul and div resp)for element by element operations. And use * / without "." for matrix operations.

  0 Comments

Sign in to comment.

More Answers (5)

Matt J
Matt J on 2 Oct 2013
y=cos(1./x)

  0 Comments

Sign in to comment.



Mugisha Aime
Mugisha Aime on 21 Sep 2017
Matrix dimensions must agree.

  1 Comment

Image Analyst
Image Analyst on 21 Sep 2017
We know the error. The reason was that they used matrix division instead of element-by-element division. In other words, he used slash
y=cos(1/x) % Not right.
instead of dot slash
y=cos(1 ./ x) % This is the fix.
as we informed Austin 4 years ago.

Sign in to comment.


moahaimen talib
moahaimen talib on 20 Mar 2017
hi please i need your help i need to process many images into wavelet function
yFolder = 'images';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
figure(k);imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
[coeffa,coeffh,coeffv,coeffd]=wavelet_process(imageArray);
i recieve this error
Error using + Matrix dimensions must agree.
Error in idwt2 (line 89) x = upsconv2(a,{Lo_R,Lo_R},sx,dwtEXTM,shift)+ ... % Approximation.
Error in wavelet_inv_process (line 5) resimg=idwt2(coeffa,coeffh,coeffv,coeffd,'haar');
Error in top_run2 (line 74) inv_wave_img=wavelet_inv_process(coeffa,imgadjh,imgadjv,imgadjd);
please SOS

  0 Comments

Sign in to comment.


ahmed reda
ahmed reda on 3 Jul 2019
Edited: Image Analyst on 3 Jul 2019
i want to know the error of this code iwant to plot w with X&Y and this error is founed
clear ;
clc;
close all;
R=0.01; %Shaft radius
m=1.675; %Disc mass (Kg)
L=0.5; %Shaft Length (m)
E=210*10^9; %Modulus of elasticity (N/m^2)
Ro=7850; %Density (Kg/m^3)
Ix=(1/4)*pi*R^4; %Moment of Inertia (Kg.m^2)
A=pi*R^2; %Cross section area (m^2)
K=48*E*Ix/L^3; %Shaft Stiffness simply supported (N/m)
Z=0.00125; %Damping ratio
C=Z*2*sqrt(K*m); %Damping constant (N/m)
e=0.059665; %eccentricity (m)
N=1500; %(rpm)
w=(0:.5:30); %rotational speed (rad/sec)
Wn=sqrt(K/m); % natural frequency
% h=0.001; %time step (sec)
g=9.81;
O=m*g/K; %Initial deflection
T=5;fs=5*8*w/(2*pi); dt=1/fs; %sampling frequency
tspan=(0:dt:T); %time interval
% X0 = [0 0]; %Initial displacement, Initial velocity in X direction
% Y0 = [O 0]; %Initial displacement, Initial velocity in y direction
% [~,X]=ode45(@abdo,tspan,X0,[],m,K,C,w,e);
% [t,Y]=ode45(@reda,tspan,Y0,[],m,K,C,w,e);
X=(m*e*(w*w))/sqrt((K-m*(w*w))^2+(C*w)^2);
Y=(m*e*(w*w))/sqrt((K-m*(w*w))^2+(C*w)^2);
plot(w,X);grid;xlabel('W(rad/sec)');ylabel('X');
figure
plot(w,Y);grid;xlabel('W(rad/sec)');ylabel('Y');
The error is:
Error using /
Matrix dimensions must agree.
Error in amplitudewithfrequency (line 21)
T=5;fs=5*8*w/(2*pi); dt=1/fs; %sampling frequency

  3 Comments

Image Analyst
Image Analyst on 3 Jul 2019
When you get an error like that you should put each line of code on it's own line:
T=5;
fs = 5*8*w /(2*pi);
dt=1/fs; %sampling frequency
so now you'll see it's 1/fs that is the problem. And fs is a vector since you made w a vector. So maybe you need to rethink that dt is. Maybe it's 1 ./ (fs(2)-fs(1)) or something like that.
Ninad Bhagwat
Ninad Bhagwat on 26 Apr 2020
Hello,
I tried using both / and ./ but I am still getting the error. Can you please tell me what should I do?
Image Analyst
Image Analyst on 27 Apr 2020
Ahmed and Ninad:
What you needed to do was to make up a list of w's and then in a for loop compute the values for each one of those w values. I believe this answers it:
clear ;
clc;
close all;
R = 0.01; %Shaft radius
m = 1.675; %Disc mass (Kg)
L = 0.5; %Shaft Length (m)
E = 210*10^9; %Modulus of elasticity (N/m^2)
Ro = 7850; %Density (Kg/m^3)
Ix = (1/4)*pi*R^4; %Moment of Inertia (Kg.m^2)
A = pi*R^2; %Cross section area (m^2)
K = 48*E*Ix/L^3; %Shaft Stiffness simply supported (N/m)
Z = 0.00125; %Damping ratio
C = Z*2*sqrt(K*m); %Damping constant (N/m)
e = 0.059665; %eccentricity (m)
N = 1500; %(rpm)
wList = (0:.5:30); %rotational speed (rad/sec)
for k = 1 : length(wList)
w = wList(k);
Wn = sqrt(K/m); % natural frequency
% h = 0.001; %time step (sec)
g = 9.81;
O = m*g/K; %Initial deflection
T = 5;
fs = 5*8*w/(2*pi);
dt = 1./fs; %sampling frequency
tspan = (0:dt:T); %time interval
% X0 = [0 0]; %Initial displacement, Initial velocity in X direction
% Y0 = [O 0]; %Initial displacement, Initial velocity in y direction
% [~,X] = ode45(@abdo,tspan,X0,[],m,K,C,w,e);
% [t,Y] = ode45(@reda,tspan,Y0,[],m,K,C,w,e);
X(k) = (m*e*(w*w))/sqrt((K-m*(w*w))^2+(C*w)^2);
Y(k) = (m*e*(w*w))/sqrt((K-m*(w*w))^2+(C*w)^2);
end
subplot(1, 2, 1);
plot(wList, X, 'b.-', 'MarkerSize', 12);
grid;
xlabel('W(rad/sec)');
ylabel('X');
subplot(1, 2, 2);
plot(wList, Y, 'r.-', 'MarkerSize', 12);
grid on;
xlabel('W(rad/sec)');
ylabel('Y');

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!