x = [16 12 14 18 13 20]; myvector = zeros (2 ,1); for ii = 1: length ( x ) if x ( ii ) > 15 myvector ( ii ) = x ( ii ); end end disp ( myvector ) in matlab

main.m
x = [16 12 14 18 13 20];
myvector = zeros(2,1);

for ii = 1:length(x)
    if x(ii) > 15
        myvector(ii) = x(ii);
    end
end

disp(myvector)
146 chars
11 lines

Output:

main.m
[16
  0]
9 chars
3 lines

The code creates a vector x with values [16 12 14 18 13 20] and initializes myvector with zeros of size [2 1]. It then loops through each element of x using a for loop. If an element is greater than 15, it assigns that value to the corresponding index in myvector. Finally, it displays the values in myvector using the disp() function. In this case, the output is [16 0], as only the first element in x is greater than 15.

gistlibby LogSnag