3

What is the fastest and the simplest way to generate an array like

[0, 1, 3, 4, 6, 7, 9, 10, ...]

in MATLAB?

4

4 回答 4

5

You can obtain the cumulative sum of the vector of steps (in your case it is [1 2 1 2 1 2 1 2 ...]). For example:

x = cumsum([0, repmat([1 2], 1, 4)])

x = 
    0     1     3     4     6     7     9    10    12
于 2013-05-02T10:49:39.540 回答
3

You can generate matrix with two rows: top row for odd array elements, bottom row for even elements. Than transform matrix into array with reshape.

>> a=[0:3:15; 1:3:16]
a =
     0     3     6     9    12    15
     1     4     7    10    13    16
>> a=reshape(a,1,12)
a =
     0     1     3     4     6     7     9    10    12    13    15    16
于 2013-05-02T10:47:14.123 回答
3

Not one line but will work for either an odd or even number of total elements, and could be expanded if you wanted more than two different steps:

a = zeros(1,8);
a(1:2:end) = 0:3:10; 
a(2:2:end) = 1:3:10;
于 2013-05-02T10:52:20.183 回答
0

Here is a simple and compact way:

A = 0:20;
A(3:3:end) = []
于 2013-05-03T11:54:49.230 回答