-2

I have a MALTAB function: find(x > [x(1)-1;x(1:n-1)] & x > [x(2:n);x(n)-1]);

How can I unvectorize this? I am not quite sure what it is even testing for!

Ideas?

4

1 回答 1

1

像这样的东西:

result = []

for i = 1:n
    if i == 1 % special case, since x(-1) does not exist
        x_below = x(1) - 1;
    else
        x_below = x(i - 1);
    end
    if i == n % special case, since x(n + 1) does not exist
        x_above = x(n) - 1;
    else
        x_above = x(i + 1);
    end
    if x(i) > x_below && x(i) > x_above
        result = [result, i]; %add found index to result
    end
end

因此,正如 Eric 所提到的,它返回 x 中大于其邻居的所有元素的索引。对于x(1),制作了 的“假”下邻x(1) - 1,因此它x(1)总是更大。因此,如果 x(1) > x(2),则返回索引 1。对x(n).

于 2013-08-08T20:18:29.763 回答