Question about how to formulate this vector

8 visualizaciones (últimos 30 días)
Tiankang Xie
Tiankang Xie el 27 de Sept. de 2015
Comentada: Image Analyst el 28 de Sept. de 2015
Hey guys,
I am having a trouble now.
I would like to create a vector, a2 = 1.0000 0.5000 0.3333 0.2500 0.2000, but I can only have "exactly one line of code".
For example, the code xx = 1 : 6 is acceptable; the code xx = [1, 2, 3, 4, 5, 6] is not.
In addition, loop is not the right choice.
Could someone help me about it?Thanks a lot!

Respuesta aceptada

Star Strider
Star Strider el 27 de Sept. de 2015
I would generate ‘a2’ simply as:
a2 = 1./[1:6]
a2 =
1 0.5 0.33333 0.25 0.2 0.16667
One line of code, as required.
  2 comentarios
Tiankang Xie
Tiankang Xie el 28 de Sept. de 2015
Thank you so much that's really awesome!
Star Strider
Star Strider el 28 de Sept. de 2015
My pleasure!
The sincerest form of appreciation here on MATLAB Answers is to Accept the Answer that most closely solves your problem.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 27 de Sept. de 2015
The line of code
xx = [1, 2, 3, 4, 5, 6]
is exactly one line of code. Why do you insist it's not?
My solution to your question is to simply do this:
a2 = [1.0000 0.5000 0.3333 0.2500 0.2000]
Note, it is definitely one single line of code.
  5 comentarios
WAT
WAT el 28 de Sept. de 2015
My best guess is that he or she was looking for a form that is easily generalizable to create an arbitrarily long vector. That's obviously far different from saying "exactly one line of code" though.
Image Analyst
Image Analyst el 28 de Sept. de 2015
If they have an integer n, then the OP can make it general by doing either of these lines:
a2 = ones(1,n) ./ [1:n]
a2 = 1 ./ [1:n]; % Parentheses also work (1:n)
whichever they prefer.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by