Question

Is it simply equivalent to 2:2:10 by ignoring the vector, or does it have any further application?

I wanted to created an array consisting of multiple numbers just like [2,3,4,6,8,9,10], but surprisingly (2:3):2:10 has returned just [2,4,6,8,10].

Était-ce utile?

La solution 2

As answered by Nick, what you wrote is interpreted by MATLAB as

2:2:10

i.e., the 3 of the first nested sequence is ignored.

What I think you wanted to accomplish was a union of two separate sequences:

>> union(2:2:10, 3:3:10)
ans =
    2     3     4     6     8     9    10

More generally,

N = 100;

C = arrayfun(@(x)x+x:x:N, 2:N, 'UniformOutput', false);
unique([C{:}])

Autres conseils

From the documentation for Colon:

If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top