Either I or matlab is going crazy with vertcat using brackets
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sam Swanson
el 13 de En. de 2017
Someone please point out what is going on with this piece of code. Matlab is running the first 3 concatenations fine, and not the last one. It's driving me crazy. It spits out
Error using vertcat Dimensions of matrices being concatenated are not consistent.
for that line. Please help. Thanks!
e = [1 1 1 1];
vertcat( 2*(e(2)*e(4) - e(3)*e(1)),2*(e(3)*e(4) + e(2)*e(1)),e(4)^2 + e(1)^2 - e(2)^2 -e(3)^2); % calling vertcat works
[ 2*(e(2)*e(4) - e(3)*e(1)) 2*(e(3)*e(4) + e(2)*e(1)) e(4)^2 + e(1)^2 - e(2)^2 -e(3)^2]; % horizontal works
% subing in works
a = 2*(e(2)*e(4) - e(3)*e(1)); b = 2*(e(3)*e(4) + e(2)*e(1)); c = e(4)^2 + e(1)^2 - e(2)^2 -e(3)^2;
[a; b; c]
% but this doesn't work for some reason...
[ 2*(e(2)*e(4) - e(3)*e(1));2*(e(3)*e(4) + e(2)*e(1));e(4)^2 + e(1)^2 - e(2)^2 -e(3)^2];
0 comentarios
Respuesta aceptada
Stephen23
el 13 de En. de 2017
Editada: Stephen23
el 13 de En. de 2017
MATLAB is doing exactly as documented:
"Blank spaces around operators such as -, :, and ( ), are optional, but they can improve readability... However, blank spaces act as delimiters in horizontal concatenation"
Because of this you have unknowingly used horzcat (via []) to generate different length vectors, which then cannot be concatenated together using vertcat. This is trivial to demonstrate, because basically you are doing this:
>> [1+2 2 -3]
ans =
3 2 -3
But I think now you can see why this is not a vector with two values, as you expect it to be. Instead, MATLAB is doing exactly as its documentation states it should. Learn to read the documentation instead of going crazy.
Your first two examples, with vertcat and horzcat are not doing what you think they are. In the case of vertcat because the input arguments are not being concatenated in themselves: they are simply interpreted as operations to be evaluated before being passed to vertcat. It is much as if you did this:
x1 = 2*(e(2)*e(4) - e(3)*e(1));
x2 = 2*(e(3)*e(4) + e(2)*e(1));
x3 = e(4)^2 + e(1)^2 - e(2)^2 -e(3)^2;
vertcat(x1,x2,x3)
because MATLAB already knows that they are input arguments to a function and are not in themselves being concatenated (this only happens inside vertcat). Perhaps what you did not check is the output of these two examples: did you notice that they have different number of terms? The reason should be clear by now.
Summary: learn to read the documentation (this is the best thing any MATLAB learner can do). And never rely on space characters to create vectors or matrices. Although loved by some, using space characters leads to exactly the problems that you see here. Much clearer is to always specify vectors and matrices using commas and semi-colons. Then the code intent is always perfectly clear:
>> [1+2 2 -3] % is this a mistake? Should it be 2-3 ?
ans =
3 2 -3
>> [1+2, 2-3] % comma makes separation clear
ans =
3 -1
2 comentarios
Guillaume
el 13 de En. de 2017
I agree with Stephen, always use commas instead of spaces to separate horizontal entries. But more importantly, be consistent with your use of spaces. I.e:
2 - 3
or
2-3
but not
2 -3
Since depending on context, the latter can be interpreted as a subtraction or as a horizontal concatenation of a positive and negative number. In my opinion, matlab should be stricter and never interpret this latter expression as a subtraction but throw an error instead when the concatenation is not contextually valid.
Más respuestas (1)
John BG
el 13 de En. de 2017
Editada: John BG
el 13 de En. de 2017
Sam
Remove all spaces in the crashing line.
[2*(e(2)*e(4)-e(3)*e(1));2*(e(3)*e(4)+e(2)*e(1));e(4)^2+e(1)^2-e(2)^2-e(3)^2]
=
0
4
0
MATLAB was attempting to take in matrix with rows of different lengths because of the way you placed the expressions, spaces, '+' and '-'.
.
Sam
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG
3 comentarios
John BG
el 13 de En. de 2017
Editada: John BG
el 14 de En. de 2017
Sam, I need the credit points, would you please be so kind to mark my answer as Accepted Answer?
As it's understandable, Mathworks protects the contents of the majority of functions.
There are a few open functions, like fmmod.m and fmdemod.m in the RF Toolbox, but to fix this spaces one may have to contact Mathworks support, report or remove all spaces unless between different matrix elements.
thanks in advance for time and attention, awaiting answer
regards
John BG
Jan
el 15 de En. de 2017
Editada: Jan
el 15 de En. de 2017
Removing all spaces from the code is a bad idea, because it impedes the readability and therefore the debugging. A general rule is to add spaces about binary operators and no space after the unary minus:
x = a + -b;
Nevertheless, I agree that the interpretation of "- 3" and "-3" is ambiguos in Matlab. Allowing the space as a separator between columns and the linebreak for rows was a bad idea from MathWorks. Without this try to be smart, there would not be a problem with spaces around the operators in arrays.
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!