use a for loop to iterate through a vector printing only the odd positioned elements. in matlab

You can use the for loop in combination with the mod function to iterate through the vector and print only the odd indexed elements. Here is an example code snippet that accomplishes that:

main.m
v = [1 2 3 4 5 6];
for i = 1:2:length(v)
    fprintf('%d\n', v(i))
end
71 chars
5 lines

In this example, we first define a vector v with some numbers. Then, we use a for loop to iterate through the vector using an index variable i that starts at 1 and increments by 2 each iteration (1:2:length(v)). We use the mod function to make sure that we only print the elements with odd index. Finally, we use the fprintf function to print each element on a new line.

Output:

main.m
1
3
5
6 chars
4 lines

related categories

gistlibby LogSnag