Create a vector using the even elements of another.

조회 수: 1 (최근 30일)
William McLemore
William McLemore 2022년 4월 27일
댓글: William McLemore 2022년 4월 27일
Hello,
I am trying to create a code to analyze Prandtl's lifting line thereom. The prompt calls for descritizing a wingspan into N nodes, where N must be an even number (100, 200, etc.). The nodes alternate "closed" and "open" (just for the sake of clarity). Each of the closed nodes are located at yj, where j = 1,3,5,...,N+1. The formula for finding the y position for every node along the wingspan is provided in the first for loop of my sample code.
Just to get my code running, I set N to 100, therefore there are 51 closed nodes. However, when I try to create a vector, yj, using only the odd elements from my y_pos vector, it alternates the correct value of yj and 0. I understand why it is doing this, but I'm at a loss on how to create a vector that only contains the odd elements from y_pos (i.e. a 51 element vector). Any help would be appreciated, thanks!
b = 10;
N = 100;
j = 1:2:N+1;
del_y = b/N;
for i = 1:1:N+1
y_pos(i) = -(b/2) + (i - 1).*del_y;
for ii = j
yj(ii) = -(b/2) + (ii - 1).*del_y;
end
end
Alternatively, this code just assigns y_pos(1) to every element yj
b = 10;
N = 100;
j = 1:2:N+1;
del_y = b/N;
for i = 1:1:N+1
y_pos(i) = -(b/2) + (i - 1).*del_y;
for ii = 1:1:(N/2)+1
yj(ii) = y_pos(i);
end
end

채택된 답변

Guido Gatti
Guido Gatti 2022년 4월 27일
Hello William,
the issue is in the indexing of your yj array. You have to distinguish between the index of the target vector (yj) and that of the source array (ypos). With reference to your first solution, instead of:
for ii=j
yj(ii) = -(b/2) + (ii - 1).*del_y;
end
a working solution would be:
for k = 1:length(j)
yj(k) = -(b/2) + (j(k) - 1).*del_y;
end

추가 답변 (1개)

DGM
DGM 2022년 4월 27일
If I understand the question correctly:
b = 10;
N = 100;
j = 1:2:N+1;
del_y = b/N;
i = 1:1:N+1;
y_pos = -(b/2) + (i - 1).*del_y; % all elements (101)
y_pos_odd = y_pos(j); % elements from odd indices (51)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by