How can I apply an operation to every row of a matrix?
9 views (last 30 days)
Show older comments
I've got a 162x2 matrix A with each row corresponding to a different point on a graph. I am looking to apply a transformation matrix R to each of those points, replacing the original points with the transformed ones.
The question is, how would I go around looping through A and then applying R to each row?
2 Comments
Accepted Answer
Jan
on 5 Dec 2022
If R is e.g. a linear transformation expressed as a [2 x 2] matrix:
coor = rand(162, 2);
a = 2 * pi / 180;
R = [cos(a) -sin(a); sin(a), cos(a)];
coor2 = (R * coor.').'; % or: coor * R.'
plot(coor(:,1), coor(:,2), 'r.');
hold('on');
plot(coor2(:,1), coor2(:,2), 'b.');
This works without a loop.
0 Comments
More Answers (0)
See Also
Categories
Find more on Language Fundamentals 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!