Contiguous Number Checking and Verification
이전 댓글 표시
Suppose, I have a variable, a.
a={ 18 5 1 3
14 2 1 2
4 2 5 9
2 1 5 7
1 5 6 7
3 3 6 1
3 7 6 5
16 6 6 3
5 3 6 6};
Based on column 3 value, I would like to check whether the values in COLUMN 4 are contiguous or not. The eg below is elaborated to clarify my goal.
Column 3 contains unique values of 1,5 and 6:
18 5 1 3
14 2 1 2
- The values in column 4 are contiguous
4 2 5 9
2 1 5 7
- The values in column 4 are NOT contiguous. (Missing '8' in Col 4)
1 5 6 7
3 3 6 1
3 7 6 5
16 6 6 3
5 3 6 6
- The values in column 4 are NOT contiguous. (Missing '2','4' in Col 4)
Result: [0 1 2]
How can I perform this complex computation? Ideally, the results should be stored using just 1 variable (if possible). My code at the moment is way too complicated and not efficient. Any help would be greatly appreciated.
댓글 수: 3
dpb
2013년 7월 4일
What results? The "missing" values? If so, it'll have to be something like a cell array since there are different number per location.
Basically, iterate over each unique value in column 3; the values in column four (apparently) are wanted to be the integer from min-max present inclusive which can simply be computed as min(x(x(:,3)==u(i),:)):max(x(x(:,3)==u(i),:))
Determine which are missing by ismember() or intersect() from the full set; whether there are any missing is simply whether the number in the range is less than that in the full set or not (assuming duplicates aren't admissible--if so, you have to do another unique() on that set to find out.
dpb
2013년 7월 9일
OK, you gave an example of what is/isn't contiguous; that helps.
But, you still haven't shown what is the specific output -- is it just a logical in which case the answer would be [1 0 0]'? Or something else?
Have to have a precise working definition to implement a solution. The general idea outlined previously works; accumarray() likely is your friend here.
RDG
2013년 7월 10일
채택된 답변
추가 답변 (1개)
dpb
2013년 7월 9일
Given the input a, the first case of returning a logical array of groups that are/aren't contiguous...
MATL
>> [u,~,c] = unique(a(:,3));
>> lcontig=accumarray(c,a(:,4),[],@(x) all(abs(diff(x))==1))
lcontig =
1
0
0
>>
Again, need more definition to know what you're actually after but should give you some ideas...
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!