# Create a vector using the even elements of another.

2 views (last 30 days)
William McLemore on 27 Apr 2022
Commented: William McLemore on 27 Apr 2022
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 on 27 Apr 2022
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
William McLemore on 27 Apr 2022
That works! Thank you so much!

DGM on 27 Apr 2022
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)

R2022a

### Community Treasure Hunt

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

Start Hunting!