Finding sequences and choose first one from that sequence

2 ビュー (過去 30 日間)
GMDI
GMDI 2019 年 2 月 15 日
コメント済み: GMDI 2019 年 2 月 17 日
I have a matrix like this :
A=[1 7; 1 8; 2 2; 2 3; 2 4; 2 8; 4 3; 4 4; 4 5; 4 6];
I want to find a matrix (say B) from A as like bellow:
B= [1 7; 2 2; 2 8; 4 3];
That means, if there is a sequence (not sure if this si the exact word to describe the problem!) in the second column of A, then I want only first element of this sequence. For example: I have 1 7 and 1 8 in A..so I want 1 7. Similarly; I have 2 2; 2 3; 2 4; 2 8..so from here I want 2 2 and 2 8. Finally, for 4 3; 4 4; 4 5; 4 6--I want 4 3.
  1 件のコメント
Kevin Phung
Kevin Phung 2019 年 2 月 15 日
編集済み: Kevin Phung 2019 年 2 月 15 日
"For example: I have 1 7 and 1 8 in A..so I want 1 7. Similarly; I have 2 2; 2 3; 2 4; 2 8..so from here I want 2 2 and 2 8. "
This is very contradicting...if youre only taking the first value for each sequence, why are you taking 2 8?
shouldn't your expected result be:
B = [1 7; 2 2; 4 3]?

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

採用された回答

Cris LaPierre
Cris LaPierre 2019 年 2 月 15 日
I agree, it was a bit confusing at first. Group by the first column and then by the max of the diff of values in the second column or 1.
To handle the case where there is a single row in a group ([2,8]), I had to specify min as an anonymous fxn so that I could specify the direction.
A=[1 7; 1 8; 2 2; 2 3; 2 4; 2 8; 4 3; 4 4; 4 5; 4 6];
G = findgroups(A(:,1),[1;max(1,diff(A(:,2)))])
f = @(x) min(x,[],1)
B = splitapply(f,A,G)
B = 4×2
1 7
2 2
2 8
4 3
  1 件のコメント
GMDI
GMDI 2019 年 2 月 16 日
Thanks Cris! This is exactly what I wanted.
@Kevin, sorry I wasn't able to explain my question clearly. Apologies for the confusion.
But Cris explained it clearly. Thank you both.

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

その他の回答 (2 件)

Cris LaPierre
Cris LaPierre 2019 年 2 月 17 日
編集済み: Cris LaPierre 2019 年 2 月 17 日
Simplifying somewhat. Try this
% find magnitude of largest number in column 1
multFac = 10^ceil(log10(max(A(:,1))))
% Plan is to combine the 2 columns with multFac*A(:,1) + A(:,2), essentially grouping them by column 1.
% Take the difference, and remove any rows with sequential values (diff==1)
% This leaves the first value of each sequence
idx = diff(multFac*[0;A(:,1)]+[0;A(:,2)])~=1;
B = A(idx==1,:)
B = 17×2
1 15
1 21
2 8
10 11
10 15
16 15
19 20
19 24
19 28
20 4
22 9
22 15
31 16
37 24
61 26
64 10
76 18
  1 件のコメント
GMDI
GMDI 2019 年 2 月 17 日
Thanks a lot. It works :)

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


GMDI
GMDI 2019 年 2 月 17 日
Hi Cris,
Sorry to bother you again. Looks like the code you provided works accurate for that small matrix A. But if my A matrix is like bellow (what I actually I have):
A=[
1 15
1 16
1 21
1 22
2 8
2 9
10 11
10 15
16 15
19 20
19 24
19 28
19 29
19 30
20 4
22 9
22 15
22 16
22 17
22 18
31 16
37 24
37 25
61 26
61 27
64 10
76 18
76 19
76 20
76 21
76 22
76 23
76 24
76 25
76 26
76 27 ];
Then I expect B as like bellow:
B=[
1 15
1 21
2 8
10 11
10 15
16 15
19 20
19 24
19 28
20 4
22 9
22 15
31 16
37 24
61 26
64 10
76 18 ];
But if we run the following code , then we get different B (Especially differences happen when 1st column element of A= 19,22,37,76......These are the four cases where we don't get what we exactly want) than what we expect.
G = findgroups(A(:,1),[1;max(1,diff(A(:,2)))]);
f = @(x) min(x,[],1);
B = splitapply(f,A,G);
The main idea is- when there is a sequence in second col, find the first element of the sequence and store it in B with it's corresponding 1st column element. If there is no sequence after an element of second column, then store it and corresponding element of 1st column in B.
Could you please take a look?

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by