필터 지우기
필터 지우기

How to identify that you have multiple values in a vector?

조회 수: 6 (최근 30일)
Larissa Monjaraz
Larissa Monjaraz 2021년 3월 12일
댓글: Walter Roberson 2021년 3월 12일
So I'm making a sort of- Yatzy-esque thing on MATLAB and I need help finding if a vector has multiple values and whether they are sequential in said vector.
Example:
[ 1 2 1 4 3 1]
Like how I be able to find that the vector has three one's and that they have numbers that go in a sequential order? Any nudge in the right direction would be appreciated.
I understand you would use functions such as find, but I'm still confused how it would be used. Sorry for being a beginner.

답변 (1개)

Basil C.
Basil C. 2021년 3월 12일
Hints:
  1. Read about unique() function
  2. For each unique value try finding the number of times it occurs using len() and find() functions
  댓글 수: 3
Basil C.
Basil C. 2021년 3월 12일
Yeah, use a for loop to 'find' the number of times each 'unique' element occurs
Walter Roberson
Walter Roberson 2021년 3월 12일
I am unclear as to the desired output about "sequential" ? Is the reasoning that in
[ 1 2 1 4 3 3 1]
  • 1 would fail because the 1's are not all adjacent to each other,
  • 2 would succeed because there are no cases where there is a 2 followed by non-2 followed by 2 ?
  • 3 would succeed because the 3's are all adjacent to each other
  • 4 would succeed because there are no cases where there is a 4 followed by a non-4 followed by 4 ?
All the 2's that are there form a continuous sequence, all the 3s that are there form a continuous sequence... the same "rule" would be used; it just needs to be clarified whether you need to have at least two of the same value (adjacent to each other) for the positions to be considered to be in sequence.

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

카테고리

Help CenterFile Exchange에서 Number games에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by