Borrar filtros
Borrar filtros

Need help writing an if statement involving vectors (2)

1 visualización (últimos 30 días)
Trevor Zane Simko
Trevor Zane Simko el 5 de Mayo de 2015
Comentada: Trevor Zane Simko el 6 de Mayo de 2015
I'm given a vector A, this vector contains five random numbers ranging from 1 to 13. I need to write an if statement to dertermine whether or not these numbers are in numerical order.
(meaning that the max(A) and min(4) have a difference of 4. And there are no repeating numbers.)
So the possibilities of A being "true" to my conditions are:
[13 12 11 10 9] [12 11 10 9 8] [11 10 9 8 7 6] [10 9 8 7 6] [9 8 7 6 5] [8 7 6 5 4] [7 6 5 4 3] [6 5 4 3 2] [5 4 3 2 1]
HOWEVER, here's the tricky part... the numbers in the given vector A can be in any order.
Any help appreciated, thank you!

Respuesta aceptada

KL
KL el 5 de Mayo de 2015
Editada: KL el 5 de Mayo de 2015
issorted(A)
returns true if they are sorted! check here
  2 comentarios
KL
KL el 5 de Mayo de 2015
if you really want to use if statements, sort the vector using sort(), then compare the sorted array with your original array (using if).
Trevor Zane Simko
Trevor Zane Simko el 6 de Mayo de 2015
I ended up just using sort() along with some other conditions. Thank you!

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 5 de Mayo de 2015
Looks like homework to me, so I will just point you in the direction of the following functions:
doc diff
doc all

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by