Extracting and sorting data in a column

조회 수: 1 (최근 30일)
Olu B
Olu B 2019년 8월 8일
편집: madhan ravi 2019년 8월 8일
Hi For example I have a 19 x1 colunmn called Mx and I need to find the maximum value in every 2 rows till the end of the column.
Mx
20
22
23
21
34
54
23
14
67
56
34
32
21
12
43
23
56
34
32
  댓글 수: 2
Alex Mcaulley
Alex Mcaulley 2019년 8월 8일
Can you show the expected result in your example?
Olu B
Olu B 2019년 8월 8일
The expected result would be
NewMx = [22 23 54 23 67 34 21 43 56 32]
Cheers

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

답변 (2개)

madhan ravi
madhan ravi 2019년 8월 8일
편집: madhan ravi 2019년 8월 8일
Works for odd as well as even number of elements:
NewMx = max(reshape([Mx(:);...
-Inf(mod(numel(Mx),2))],2,[])) % thank you Guillaume
  댓글 수: 7
Jos (10584)
Jos (10584) 2019년 8월 8일
My apologies Madhan!
madhan ravi
madhan ravi 2019년 8월 8일
No problem Jos :)

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


Jos (10584)
Jos (10584) 2019년 8월 8일
This works for both an even or an odd number of elements:
N = 11 ; % odd
Mx = randi(10, N, 1)
M2 = accumarray(ceil((1:numel(Mx))/2).', Mx, [], @max)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by