Create and rotate a vector field out of two matrices (x and y component)

2 views (last 30 days)
Dear all,
my questions referes to the rotation of a vector field that consists of two matrices (data2_va1 and data2_var2). The dimenson of both matrices is 61x43.
Using a minimal exampe, it works out quite fine.
v=[data2_var1(1,1),data2_var2(1,1)];
theta = 90;
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
vR=v*R;
However, trying to use the piece of code to create a vector field v results in the following error message: Subscripted assignment dimension mismatch. Maybe somebody could give me a hint, how to construct a proper vector field out of two matrices (x and y component are seperated in the matrices data2_var1 and data2_var2).
for i=1:size(data2_var1,1)
for j=1:size(data2_var1,2)
v(i,j)=[data2_var1(i,j),data2_var2(i,j)];
end
end
theta = 90; R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)]; v1R(i,j)=v1(i,j)*R;
Thanks a lot in advance, Fernandes

Accepted Answer

Roger Stafford
Roger Stafford on 22 Jun 2013
For brevity call X = data2_var1 and Y = data2_var2. Do things this way:
vR=[X(:),Y(:)]*R;
X2 = reshape(vR(:,1),size(X));
Y2 = reshape(vR(:,2),size(Y));
X2 and Y2 constitute the x and y components of your rotated field.
  2 Comments
G
G on 22 Jun 2013
Great job. Thank you very much for your straight forward solution.
Roger Stafford
Roger Stafford on 22 Jun 2013
I notice that the way you have set up your rotation matrix R for multiplication on the right, a positive angle would give clockwise rotation and negative, counterclockwise. Is that the way you intend? I am accustomed to the opposite convention.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!