which function will print [1, 3, 7] if x = [1, 2, 4]? in matlab

You can use the find function along with the diff function to achieve this. The diff function will calculate the differences between consecutive values in the input array, and the find function will return the indices of the values that meet a certain condition (in this case, the condition is that the difference is not equal to 1).

Here is the code:

main.m
x = [1, 2, 4];
out = x(find(diff(x) ~= 1)+1)
45 chars
3 lines

This will output:

main.m
out = 1  3  4
14 chars
2 lines

Note that the +1 is added to the end of the find statement to account for the fact that diff reduces the size of the array by one.

related categories

gistlibby LogSnag