Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Error due to matrix dimensions not being consistent

1 visualización (últimos 30 días)
micheal marembo
micheal marembo el 17 de Feb. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hello everyone,
Below is a code i am trying to write. When I run it, I get errors due to matrix dimensions. May I know how to solve such errors please,thanks.
function h = mtimes(u,v)
% Multiplication with matrix or scalar: `h = u*v`
if ~isa(u,'ADI') %u is a scalar/matrix
h = v;
h.val = u.*(h.val);
h.jac = ADI.mtimesJac(u, h.jac);
elseif ~isa(v,'ADI') %v is a scalar
h = mtimes(v,u);
else % special case where either u or v has single value
if numel(u.val) == 1
h = u;
h.val = times(u.val, v.val);
h.jac = ADI.timesJacUnit(u.val, v.val, u.jac, v.jac);
elseif numel(v.val) == 1
h = u;
h.val = times(u.val, v.val);
h.jac = ADI.timesJacUnit(v.val, u.val, v.jac, u.jac);
else
error('Operation not supported');
end
end
end
the errors are;
Error using .*
Matrix dimensions must agree.
Error in * (line 161)
h.val = u.*(h.val);
  3 comentarios
micheal marembo
micheal marembo el 18 de Feb. de 2020
Editada: Rik el 18 de Feb. de 2020
Hello, attached is the code for the ADI class and the persistent errors are also attached at the end of the code, Thank you.
Edit Rik: moved class definition to attachment
The following are the errors;
Error in * (line 162)
h.val = u*(h.val);
Error in .* (line 191)
h = mtimes(u,v);
Thank you
Rik
Rik el 18 de Feb. de 2020
To keep the thread readable I moved the classdef to an attachment and edited your comment for you. Next time, please use the tools explained on this page to make your posts more readable.
Can you provide the code you are using to reproduce this error? (also, posting code in this forum sometimes removes blank lines, so the line numbers in your errors don't line up with the code anymore.

Respuestas (1)

Rik
Rik el 17 de Feb. de 2020
Editada: Rik el 17 de Feb. de 2020
You have shadowed the internal function mtimes which is used when you use the * operator. Renaming your function should solve the issue.
  3 comentarios
Rik
Rik el 17 de Feb. de 2020
If you have any remaining errors, feel free to comment, if not please feel free to mark this as accepted answer.
micheal marembo
micheal marembo el 17 de Feb. de 2020
I changed the function name but the same errors are still persisting.

Community Treasure Hunt

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

Start Hunting!

Translated by