필터 지우기
필터 지우기

Need help writing an if statement involving vectors (2)

조회 수: 1 (최근 30일)
Trevor Zane Simko
Trevor Zane Simko 2015년 5월 5일
댓글: Trevor Zane Simko 2015년 5월 6일
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!

채택된 답변

KL
KL 2015년 5월 5일
편집: KL 2015년 5월 5일
issorted(A)
returns true if they are sorted! check here
  댓글 수: 2
KL
KL 2015년 5월 5일
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 2015년 5월 6일
I ended up just using sort() along with some other conditions. Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by