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.m71 chars5 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.m6 chars4 lines
gistlibby LogSnag