Error using == Matrix dimensions must agree.

Hi, I want a solution to this problem. Someone can help me?
Error in operations (line 5)
elseif c == 'multi'

 Réponse acceptée

Star Strider
Star Strider le 30 Mai 2015
I have no idea what the matrix dimension problem is, since we have only one line of your code. However, the way to compare strings and string variables is to use the strcmp or strcmpi functions.
So the correct syntax for the line you posted would be:
elseif strcmpi(c, 'multi')

7 commentaires

Alvaro Arenas
Alvaro Arenas le 30 Mai 2015
Modifié(e) : Star Strider le 30 Mai 2015
Hi, Star Strider. Thank you for answer my question
I corrected the matlab code but now I have this problem
>> o1 = operations(4,'suma')
o1 =
8
>> o2 = operations(4,'multi')
Error in operations (line 3)
if strcmpi(c, 'suma')
Output argument "o1" (and maybe others) not assigned during call to
The logic in your code is correct. I experimented with all the options.
You can realistically have only one output, not three, so I would change your code to:
function [out] = operations(M,c)
out = NaN;
if strcmpi(c, 'suma')
out = M+M;
elseif strcmpi(c, 'multi')
out = M*M;
elseif strcmpi(c, 'expo')
out = M^M;
end
end
That should eliminate the error.
The initial assignment setting out to NaN covers the error condition when nothing matches ‘c’ correctly. The operation would not be defined, so that would actually be appropriate.
Alvaro Arenas
Alvaro Arenas le 31 Mai 2015
Star Strider Thank you so much. I resolved this problem
Star Strider
Star Strider le 31 Mai 2015
My pleasure.
If my Answer helped you resolve your problem, please Accept it.
KenoKanawa
KenoKanawa le 16 Nov 2018
Thank you, it solved my problem too !
Star Strider
Star Strider le 16 Nov 2018
@KenoKanawa — My pleasure!
Dunzhi Chen
Dunzhi Chen le 25 Juin 2020
Thanks, this solved my problem!

Connectez-vous pour commenter.

Plus de réponses (1)

Pronoy Das
Pronoy Das le 25 Fév 2021
Modifié(e) : Pronoy Das le 25 Fév 2021

2 votes

I came across a similar problem. Simply replace the single quotation marks with the double quotation marks, i.e. replace the line in your code with
c == "multi"

Catégories

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by