Matlab Function - Error using => horzcat

Click For Summary
SUMMARY

The forum discussion addresses an error encountered in a MATLAB function, specifically "Error using ==> horzcat," which arises when attempting to concatenate matrices with inconsistent dimensions. The function in question, rotatex, is designed to rotate a vector in by a specified angle using a rotation matrix m. The error indicates that the dimensions of the input matrix do not match the expected dimensions for matrix multiplication, particularly when trigonometric functions are involved. The solution requires ensuring that the input vector in is correctly defined and compatible with the rotation matrix.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with matrix operations in MATLAB
  • Knowledge of trigonometric functions and their usage in MATLAB
  • Experience with debugging MATLAB code and error messages
NEXT STEPS
  • Review MATLAB matrix dimension rules and the implications for matrix multiplication
  • Learn about MATLAB's horzcat function and its usage for concatenating matrices
  • Investigate the proper definition and initialization of input vectors in MATLAB functions
  • Explore debugging techniques in MATLAB to identify and resolve function errors
USEFUL FOR

MATLAB developers, data scientists, and engineers working with matrix computations and transformations who need to troubleshoot and optimize their code for matrix operations.

mycroft
Messages
7
Reaction score
0
Code:
function out = rotatex(angle, in)
m=[1 0 0 0; 0 cos(angle) -sin(angle) 0; 0 sin(angle) cos(angle) 0; 0 0 0 1];
out = m*in;

And the call:

Code:
>> out = rotatex(in, pi/4);
? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Error in ==> rotatex at 2
m=[1 0 0 0; 0 cos(angle) -sin(angle) 0; 0 sin(angle) cos(angle) 0; 0 0 0 1];

I've counted the elements, tested the declaration outside the function, and it works. I've removed the minus before the sin. I've declared the rows separately and tried calling horzcat explicitly and as soon as the trig functions are included it throws a hissyfit..but only when it is called from within the function.

Help much appreciated. In the meantime I'll just head off on a murderous rampage...wanders off muttering expletives.
 
Physics news on Phys.org
The problem exists in the fact that you've not used the horzcat function anywhere in the codes you've shown. What is in in the function call? I tried the code using angles in the function call and got no problem. The problem should exist in the code you've not shown us.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K