Extracting and sorting data in a column

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 日

0 投票

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 日
nice, but only works when the number of elements in Mx is even
madhan ravi
madhan ravi 2019 年 8 月 8 日
編集済み: madhan ravi 2019 年 8 月 8 日
Hä? Jos did you even try the solution before claiming??
>> Mx = 1:11
Mx =
1 2 3 4 5 6 7 8 9 10 11
>> NewMx = max(reshape([Mx(:);...
zeros(mod(numel(Mx),2))],2,[]))
NewMx =
2 4 6 8 10 11
>>
Guillaume
Guillaume 2019 年 8 月 8 日
Since we're looking at the max, I'd recommend padding with -Inf rather than 0:
NewMx = max(reshape([Mx(:); -Inf(mod(numel(Mx),2), 1)],2,[]))
madhan ravi
madhan ravi 2019 年 8 月 8 日
Thank you Guillame :)
Guillaume
Guillaume 2019 年 8 月 8 日
By the way, the mod expression should be:
mod(-numel(Mx), groupsize) %note the -
When groupsize is 2, - or + give the same result. For larger groupsize it's critical.
But the idea is there, when you want to group consecutive elements of a vector, the solution is to reshape the vector so that each group is row/column.The idea extends to higher dimensions as well. If you want to calculate something for every 3 consecutive columns of a matrix, reshape the matrix to 3D to have 3 columns and ncolumns/3 pages.
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 日

0 投票

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)

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

質問済み:

2019 年 8 月 8 日

編集済み:

2019 年 8 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by