Creating matrix inside indexed for loop
조회 수: 18 (최근 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
rotations = rotations + (theta(i)/(2*pi));
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!