Cross products with anonymous functions

2 views (last 30 days)
I please need help with my code. I want to end up with a matrix. I have an anonymous function which I would like to pass various values (in a vector form) and then I want the outputs in a matrix form.
n = 4;
A = @(th) [-R.*cos(th),-R.*sin(th), 0];
theta = 0:pi/2:2*pi;
Lets say I have values for theta of 0:pi/n:2*pi; When I evaluate the function I get this:
u = 1×9
-0.0500 -0.0250 0.0250 0.0500 0 -0.0433 -0.0433 -0.0000 0
I understand that the vector has the answer vectors next to each other, but where are the intermediate 0's? I would expect the answer to have atleast 12 values.
This is so that I could cross product it with other matrices later on.
Any help would be much appreciated!

Accepted Answer

Anton Gribovskiy
Anton Gribovskiy on 23 Sep 2019
If you sure that your th will always be 1-by-n vector, you just need to put semicolons instead of comas for vertical cat:
A = @(th) [-R.*cos(th);-R.*sin(th); zeros(1, length(th))];
Or, if you want to make it working for vertical and horizontal vectors
A = @(th) [-R.*cos(th(:).');-R.*sin(th(:).'); zeros(1, length(th))];
  1 Comment
Jonathan Rabe
Jonathan Rabe on 23 Sep 2019
Thank you so much! I have been struggling to get this to work for hours now.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!