# How to reshape based on like values?

6 ビュー (過去 30 日間)
Dc215905 2021 年 7 月 10 日
コメント済み: Walter Roberson 2021 年 7 月 10 日
Hello,
I have the following example array:
A = [1 1 1 2 2 3 3 3 3 3 5]
I would like to rehsape it into a matrix based on like values. Since the values are not uniform, I would like toi fill in the rest with Nan. The ultimate goal is to have it look like:
A =
1 2 3 5
1 2 3 NaN
1 NaN 3 NaN
NaN NaN 3 NaN
NaN NaN 3 NaN
Any suggestions?
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2021 年 7 月 10 日
Suppose there had been two more 1 after the 3s and before the 5. What result would you want then?

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

### 回答 (1 件)

Simon Chan 2021 年 7 月 10 日
If I guess the logic correctly:
A = [1 1 1 2 2 3 3 3 3 3 5];
N = histcounts(A);
rowA = max(N);
B= unique(A);
colA = length(B);
C = NaN(rowA,colA);
E = N(B);
for k=1:colA
C(1:E(k),k)=B(k);
end
C =
1 2 3 5
1 2 3 NaN
1 NaN 3 NaN
NaN NaN 3 NaN
NaN NaN 3 NaN
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2021 年 7 月 10 日
This is why I inquired about what should happen if there were more ones later: that information would tell us whether order needs to be preserved.

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

### カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by