Error using / Matrix dimensions must agree...

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 comentario

guadalupe presa
guadalupe presa el 22 de Sept. de 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)

Iniciar sesión para comentar.

 Respuesta aceptada

Amit Nambiar
Amit Nambiar el 2 de Oct. de 2013

15 votos

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.

Más respuestas (6)

Mugisha Aime
Mugisha Aime el 21 de Sept. de 2017

1 voto

Matrix dimensions must agree.

3 comentarios

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.
Muhammad Azrul Izmee
Muhammad Azrul Izmee el 27 de En. de 2022
This is not right too.. I hv do it but keep getting error
@Muhammad Azrul Izmee, it is right. Here's proof
x=-1:0.0001:1;
y=cos(1 ./ x)
y = 1×20001
0.5403 0.5402 0.5401 0.5400 0.5400 0.5399 0.5398 0.5397 0.5396 0.5395 0.5395 0.5394 0.5393 0.5392 0.5391 0.5390 0.5390 0.5389 0.5388 0.5387 0.5386 0.5385 0.5384 0.5384 0.5383 0.5382 0.5381 0.5380 0.5379 0.5379
plot(x, y, 'b-')
See? No error.
You should start a new question with your own code.

Iniciar sesión para comentar.

moahaimen talib
moahaimen talib el 20 de Mzo. de 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
ahmed reda
ahmed reda el 3 de Jul. de 2019
Editada: Image Analyst el 3 de Jul. de 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

5 comentarios

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 el 26 de Abr. de 2020
Hello,
I tried using both / and ./ but I am still getting the error. Can you please tell me what should I do?
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');
Rafli Noorsyaif
Rafli Noorsyaif el 17 de En. de 2021
clc
clear all
close all
%Diketahui (E1,E2,G12 dalam satuan MPa)
E = 180000;
SIGMAY = 502;
%S,A,W dalam satuan m
S = 0.1;
a = 0.005;
W = 0.01:0.01:0.1;
K = ((3*(S/W)*sqrt(a/W))/(2*(1+2*(a/W))*((1-(a/W))^(3/2))))*(1.99-(a/W)*(1-(a/W)))*(2.15-3.93*(a/W)+2.7*((a/W)^(2)))
Displacement = (K^(2))/(E*SIGMAY)
I have the same problem (Matrix dimensions must agree) on the line 14
K = ((3*(S/W)*sqrt(a/W))/(2*(1+2*(a/W))*((1-(a/W))^(3/2))))*(1.99-(a/W)*(1-(a/W)))*(2.15-3.93*(a/W)+2.7*((a/W)^(2)))
Rafli, usually what many/most people do in situations like this, which have an incredibly complicated and involved equation, is to break it down into smaller parts, then examine the size of each term. Like
term1 = 3*S/W;
term2 = sqrt(a/W);
and so on. Just basic debugging technique.
Then make sure all your matrix multiplication dimensionss of each term make sense, and also make sure you really want to do a matrix multiplication with star and not an element-by-element multipliication with dot star.For example since you have term1*term2, that means the number of columns in term1 must equal the number of rows in term1 for a matrix multiplication since term1 is on the left side.
Start your own question if you still have trouble.

Iniciar sesión para comentar.

Rahayu A Halim
Rahayu A Halim el 8 de Ag. de 2021
Hello, please I need your help
my script:
%____________________________________________________________________
function fx=obj_pid_ga(x)
global Kp Ki Kd
%
%
Kp=x(1);Ki=x(2);Kd=x(3);
%
%____________________________________________________________________
[t,~,y]=sim('model_sistem_PID',0:0.05:10);
%____________________________________________________________________
%Integral of Squared Error
fx=sum(t.*abs(y).^2);
May I know why I keep getting this error:
Error using .*
Matrix dimensions must agree.
Error in obj_pid_ga (line 10)
fx=sum(t.*abs(y).^2);

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 2 de Oct. de 2013

Comentada:

el 27 de En. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by