Matrix size mismatch Matlab Function
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Laura
el 28 de Mzo. de 2023
Comentada: Laura
el 28 de Mzo. de 2023
Hi,
I am trying to code this MATLAB Function where I am working with vectors:
function [Wp,S] = fcn(x, vel, Fext)
r=[Fext;x;vel];
p=50;
c=0.2;
mu=c*ones(1,p);
sigma=1;
alpha=0.01;
S=ones(p,1);
for j=1:p
S(j)=exp(-norm(r-mu(:,j))^2/(2*sigma^2));
end
Wp=alpha*Fext*S;
Wp=Wp';
I am getting this following error, but I don't get why there is a mismatch:
Size mismatch (size [1 x 1] ~= size [50 x 1]). The size to the left is the size of the left-hand side of the assignment.
Thanks!
0 comentarios
Respuesta aceptada
Torsten
el 28 de Mzo. de 2023
Movida: Torsten
el 28 de Mzo. de 2023
S(j) is a single value (1x1).
exp(-norm(r-mu(:,j))^2/(2*sigma^2)) is a vector (50x1).
You try to save a vector in a scalar value:
S(j)=exp(-norm(r-mu(:,j))^2/(2*sigma^2));
Error.
5 comentarios
Torsten
el 28 de Mzo. de 2023
Editada: Torsten
el 28 de Mzo. de 2023
How do you call the function (i.e. what is r) ?
This code works, e.g.:
x=1;
vel=1;
Fext = 1;
[Wp,S] = fcn(x, vel, Fext)
function [Wp,S] = fcn(x, vel, Fext)
r=[Fext;x;vel];
p=50;
c=0.2;
mu=c*ones(size(r,1),p);
sigma=1;
alpha=0.01;
S=ones(p,1);
for j=1:p
S(j)=exp(-norm(r-mu(:,j))^2/(2*sigma^2));
end
Wp=alpha*Fext*S;
Wp=Wp';
end
Más respuestas (0)
Ver también
Categorías
Más información sobre Sources en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!