Hi Everyone,
I have a problem that I'm slightly struggling with.
I have two arrays which represent the time step (A) and the corresponding iterations at each time step (B).
A = [1 1 1 2 2 2 3 3 3 3 4 4 4 4 4]
B = [1 2 3 1 2 3 1 2 3 4 1 2 3 4 5]
I would like to extract the maximum iteration (from B) for each time step (from A).
So it should look like
C = [1 2 3 4]
D = [3 3 4 5]
Any help is greatly appreciated.
Best regards,
Lloyd

 채택된 답변

Stephen23
Stephen23 2018년 8월 21일

2 개 추천

>> A = [1 1 1 2 2 2 3 3 3 3 4 4 4 4 4];
>> B = [1 2 3 1 2 3 1 2 3 4 1 2 3 4 5];
>> C = unique(A)
C =
1 2 3 4
>> D = accumarray(A(:),B(:),[],@max)
D =
3
3
4
5

댓글 수: 4

Lloyd Sullivan
Lloyd Sullivan 2018년 8월 21일
Thank you for the prompt response Stephen, it works perfectly! I never knew of this accumarray command, I have learnt something new :)
That works as long as the elements in A and B are positive integer values. Using findgroups and splitapply works when A and B contain positive integer values:
A = [1 1 1 2 2 2 3 3 3 3 4 4 4 4 4];
B = [1 2 3 1 2 3 1 2 3 4 1 2 3 4 5];
G = findgroups(A);
results = splitapply(@max, B, G)
It also works if they contain something other than positive integer values:
A2 = A./4;
G2 = findgroups(A2);
results = splitapply(@max, B, G2)
Operating on A2 using accumarray will throw an error:
D = accumarray(A2(:), B(:), [], @max)
And Steve's answer does not work in the general case where the numbers in B can be anything, like even negative numbers. Try this and see:
A = [1 1 1 2 2 2 3 3 3 3 4 4 4 4 4];
B = rand(1, length(A)) - 0.5
G = findgroups(A);
results = splitapply(@max, B, G)
If you want something what works for any values of A and any values of B, regardless of sorting order or positive of negative values, it's a few more lines of code. Hint: one way is to use regionprops() in the Image Processing Toolbox.
Image Analyst, to which of us were you referring when you said "Steve's answer"? If it was to me, the code you posted works when I try it in release R2018a.
>> rng default
>> A = [1 1 1 2 2 2 3 3 3 3 4 4 4 4 4];
>> B = rand(1, length(A)) - 0.5;
>> G = findgroups(A);
>> results = splitapply(@max, B, G);
>> [A.', B.'], results
ans =
1.0000 0.3147
1.0000 0.4058
1.0000 -0.3730
2.0000 0.4134
2.0000 0.1324
2.0000 -0.4025
3.0000 -0.2215
3.0000 0.0469
3.0000 0.4575
3.0000 0.4649
4.0000 -0.3424
4.0000 0.4706
4.0000 0.4572
4.0000 -0.0146
4.0000 0.3003
results =
0.4058 0.4134 0.4649 0.4706
Do you see different answers when you run those six lines of code? If so what release are you using?

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

추가 답변 (1개)

Adam Danz
Adam Danz 2018년 8월 21일

1 개 추천

Differentiate B in order to know when the count restarts. Then use the index of the restart-points to get C and D.
maxIdx = diff([B,-1])<0; % -1 needed to capture last value
C = A(maxIdx);
D = B(maxIdx);

댓글 수: 2

Adam Danz
Adam Danz 2018년 8월 21일
편집: Adam Danz 2018년 8월 21일
Here's an alternative solution if speed is an issue (both of these solutions are fast but this one is faster).
Lloyd Sullivan
Lloyd Sullivan 2018년 8월 21일
Very efficient - almost twice as fast. Thanks you Adam.

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

카테고리

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

제품

릴리스

R2016a

태그

질문:

2018년 8월 21일

댓글:

2018년 8월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by