I want to index a element of a vector after defining it

1 visualización (últimos 30 días)
Eduardo
Eduardo el 7 de Oct. de 2024
Movida: Rik el 7 de Oct. de 2024
I want to select the first element of a vector without having to store it in a variable. For example, if i want to know the smallest prime divider of a number, i want to write this
factor(56)(1)
However this is invalid. Any correct way to do this?

Respuesta aceptada

Stephen23
Stephen23 el 7 de Oct. de 2024
Movida: Rik el 7 de Oct. de 2024
"Any correct way to do this?"
Yes, but only if you want to make your code complex, awkward, and obfuscated:
subsref(factor(56),substruct('()',{1}))
ans = 2
struct('x',factor(56)).x(1)
ans = 2
The MATLAB approach: store it in a vector. Note that how many lines you use is not an indicator of runtime efficiency.
  1 comentario
Eduardo
Eduardo el 7 de Oct. de 2024
Movida: Rik el 7 de Oct. de 2024
Thanks, i guess i will store it in a vector

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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