How do you find the longest consecutive values of a number in an array?

you can only use the functions: length, size, max, min, mod, all, any, sort, find, floor, ceil, round, log10, sum, zeros, ones, reshape, repmat

댓글 수: 3

Beside the fact, that you cannot create a useful program with these commands only, I even do not understand the question. What exactly are "longest consecutive values"?
mal t
mal t 2015년 11월 29일
편집: mal t 2015년 11월 29일
say you have an array [1 1 1 0 0 0 1 1 1 1 1] . I'm looking here for the longest consecutive value of 1, which is 5.
It's my fault for not stating that you can use <= , < , >= , > , ~ , ~= , == + , - , * , / , ^ , *. , /. , ^. .
but those can be used.
Since it's homework we can't just give the you complete answer outright. Since you haven't given any code for us to fix, I'm not sure what hints to even give other than to use those functions and operators you were told you were allowed to use. I'm going to assume the numbers are integers and I'm going to assume you can use indexing like vec(2:end), so I'd look closely at the subtraction operator.

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

답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 29일

1 개 추천

That cannot be done. In order to find consecutive values you would need to index into the array, but indexing is a function in MATLAB and it is not on your permitted list.

댓글 수: 2

Even the assignment of variables will suffer from the excluded subsasgn. And without any function for an output there is no chance to show the result.
You are allowed to use logical indexing like >, <, == etc. And + * - etc. But that's all

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

Guillaume
Guillaume 2015년 11월 29일
The simplest way to do what you want is with:
max(diff(find(diff([NaN v NaN])))) %where v is the input vector
It is trivial to replace diff by some simple indexing. The function NaN can be replaced by any number known not to occur in the vector. NaN is the safest value to use though.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2015년 11월 29일

답변:

2015년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by