Grouping elements in a matrix

I have a matrix, lets call it M. The elements of M have values between -2 to +2. For example, M is a row matrix,
and it is given by
M = [2 1.3 1.4 1.1 -1 -0.9 -0.5 0 0.5 0.9 1.1 0.9 1.6 0.8 0.2 1.1....]
I would like to sort the elemnts according to: .
i.e., in the above example, the grouping should look like this: {2 1.3 1.4 1.1 -1}, { -0.9 -0.5 0 .5 0.9},{1.1}, { 0.9 }, {1.6},{0.8 0.2}, {1.1}..
Similarly, I would like to do this kind of sorting at each row for a matrix M(i,j).
Can anyone please help me how to do it in MATLAB?
Thanks!

4 件のコメント

Voss
Voss 2023 年 1 月 20 日
Why is the grouping not
{2 1.3 1.4 1.1 -1}, { -0.9 -0.5 0 .5 0.9}, {1.1}, {0.9}, {1.6}, {0.8 0.2}, {1.1}, ...
?
Anjan Dadgupta
Anjan Dadgupta 2023 年 1 月 20 日
Thanks for pointing out.
It has been modified now.
Matt J
Matt J 2023 年 1 月 20 日
編集済み: Matt J 2023 年 1 月 20 日
I for one do not understand the grouping criterion. Are you just grouping monotonically increasing/decreasing runs of numbers together? How should the final result be stored if there are a different number of groups in each row of M? And what do you plan to do with the groups once you've isolated them?
Fangjun Jiang
Fangjun Jiang 2023 年 1 月 20 日
M = [2 1.3 1.4 1.1 -1 -0.9 -0.5 0 0.5 0.9 1.1 0.9 1.6 0.8 0.2 1.1];
index=and(M>-1, M<1)
index = 1×16 logical array
0 0 0 0 0 1 1 1 1 1 0 1 0 1 1 0
For one row of data, I think the logic for grouping is to check the change of value in "index" above

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

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 1 月 20 日

0 投票

M = [2 1.3 1.4 1.1 -1 -0.9 -0.5 0 0.5 0.9 1.1 0.9 1.6 0.8 0.2 1.1];
index=and(M>-1, M<1);
idx=unique([1 find(diff(index)~=0)+1 numel(M)+1]);
out=mat2cell(M,1,diff(idx))
out = 1×7 cell array
{[2 1.3000 1.4000 1.1000 -1]} {[-0.9000 -0.5000 0 0.5000 0.9000]} {[1.1000]} {[0.9000]} {[1.6000]} {[0.8000 0.2000]} {[1.1000]}

カテゴリ

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

製品

リリース

R2020b

タグ

質問済み:

2023 年 1 月 20 日

回答済み:

2023 年 1 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by