Pergunta

Is there an easier way to create a such a vector b=[1,3,5,7,9,7,5,3,1]?

What I did was, I basically divided the vector into the increasing and decreasing parts and used horzcat as follows:

a=horzcat((1:2:9),(7:-2:1));

disp(a);

However, this doesn't seem very efficent. Is there a simpler way of doing this, without having to declare and concatanate two vectors?

Thanks.

Foi útil?

Solução

It's not terribly inefficient, no. However, you don't need to call horzcat by name:

a = [1:2:9 7:-2:1]

But if you want to avoid the second set of colon operators, you could flip the first side:

a = 1:2:9;
a = [a fliplr(a(1:end-1))]

But there you have another colon, and it's definitely more efficient to do it the first way.

Possibly by c = 9; s = 2; b = 1:s:c-2; a = [b c fliplr(b)]; if c-1 is divisible by s, but I don't see the point of complicating a simple operation like this... unless this is how you get your kicks. ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top