Creating matrix inside indexed for loop

10 次查看(过去 30 天)
I would like to know how to create a matrix inside a for loop with indexing. The indexing causes an array on its own, but I already have a vector (V, U and W) inside the for loop. Now I would like to create a matrix that shows the vector at each timestamp, but it gives the error : Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
To my knowledge, using a ' ; ' inside a vector makes it jump to the next row instead of using a ' , ' to jump to the next column. That's why I used it inside the vectors V and U. Because then the time vector would jump to the next column each iteration of the for loop, leaving a nice 2 by 1002 matrix, but as said before, it gave an error
This is my code:
%% Dimensions VAWT
R = 0.5;
H = 1.5;
%% Speeds
labda = 4;
V = [0;1];
theta(1) = 0;
omega = norm(V)*labda/R;
U_magnitude = omega*R;
%% Variables
i(1) = 1;
dt = 0.01;
rotations = 0;
%% Calculation
for t = 0:dt:10
i = i+1;
theta(i) = theta(i-1) + (omega*dt);
if theta(i) <= (2*pi)
theta(i) = theta(i-1) + (omega*dt);
else
theta(i) = theta(i) - (2*pi);
rotations = rotations+1;
end
angle_U(i) = theta(i) + (0.5*pi);
U(i) = [cos(angle_U(i))*U_magnitude;sin(angle_U(i))*U_magnitude];
W(i) = V - U(i);
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
rotations = rotations + (theta(i)/(2*pi));

采纳的回答

Mathieu NOE
Mathieu NOE 2020-10-29
hi
as far as I understand U,V,W have 2 rows , so if I am right, this is the way to modifiy your code
%% Dimensions VAWT
R = 0.5;
H = 1.5;
%% Speeds
labda = 4;
V = [0;1];
theta(1) = 0;
omega = norm(V)*labda/R;
U_magnitude = omega*R;
%% Variables
i(1) = 1;
dt = 0.01;
rotations = 0;
%% Calculation
for t = 0:dt:10
i = i+1;
theta(i) = theta(i-1) + (omega*dt);
if theta(i) <= (2*pi)
theta(i) = theta(i-1) + (omega*dt);
else
theta(i) = theta(i) - (2*pi);
rotations = rotations+1;
end
angle_U(i) = theta(i) + (0.5*pi);
U(:,i) = [cos(angle_U(i))*U_magnitude;sin(angle_U(i))*U_magnitude];
W(:,i) = V - U(:,i);
end
  1 个评论
Luc Kuijken
Luc Kuijken 2020-10-29
oke, thank you. So I just needed to double index the U and the W. Which is quite logical come to think obout it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by