フィルターのクリア

Contiguous Number Checking and Verification

6 ビュー (過去 30 日間)
RDG
RDG 2013 年 7 月 4 日
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
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
RDG 2013 年 7 月 10 日
It isn't a logical. If you notice, it's 0,1 and 2. 0 because there's no missing value (contiguous), 1 because it's missing the value 8, and 2 because it is missing the value 2 and 4.

サインインしてコメントする。

採用された回答

dpb
dpb 2013 年 7 月 10 日
編集済み: dpb 2013 年 7 月 10 日
OK, just change the function slightly...
MATL
>> [~,~,c] = unique(a(:,3));
>> nmiss=accumarray(c,a(:,4),[],@(x) length([min(x):max(x)])-length(x))
nmiss =
0
1
2
>>
And, to anticipate the next wishes/needs (*) ...
MATL
>> [u,~,c] = unique(a(:,3));
>> groups = accumarray(c,a(:,4), [length(u) 1], @(x) {x})
groups =
[2x1 double]
[2x1 double]
[5x1 double]
>> groups{:}
ans =
3
2
ans =
9
7
ans =
7
1
5
3
6
>> missing = accumarray(c,a(:,4), [length(u) 1], ...
@(x) {setdiff(min(x):max(x), x)})
missing =
[1x0 double]
[ 8]
[1x2 double]
>> missing{:}
ans =
Empty matrix: 1-by-0
ans =
8
ans =
2 4
>>
(*) And credit to Kelly Kearney at cs-sm who showed the "trick" to getting the SZ argument correct for the cell array addressing for the last two. Thanks, Kelly!!! :)
  1 件のコメント
RDG
RDG 2013 年 7 月 11 日
編集済み: RDG 2013 年 7 月 11 日
Thanks for the trouble! This has helped me a lot!

サインインしてコメントする。

その他の回答 (1 件)

dpb
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...

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by