Rearrange a matrix by the values of the first column

1 回表示 (過去 30 日間)
Day Hong Kim
Day Hong Kim 2020 年 12 月 17 日
コメント済み: Day Hong Kim 2020 年 12 月 18 日
Hello all, I would like to rearrange the matrix according to the values in the first column. the first column represents station number and the second column represents value acquired from the corresponding station ( I attached the file). I have 9 stations, so I want a n x 9 matrix where n is the values from each station. It would be like
0 0 0 0
10 10 10 10
20 20 20 20
30 30 30 30
50 50 50 50
75 75 75 75
125 125
150 150
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 17 日
If you want help, you need to make it easy to undestand. Please elaborate the question with small sample example
Day Hong Kim
Day Hong Kim 2020 年 12 月 18 日
Sorry for my bad writing skill. Here is the example,
The first column is the number of each station and the second column is the value acquired from the station.
1 0
1 10
1 20
2 0
2 10
2 20
2 30
I want to rearrange this matrix to
0 0
10 10
20 20
30
so each column represents a station and the rows represent the value.
Hope this makes sense.

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

採用された回答

Matt J
Matt J 2020 年 12 月 17 日
load(websave('question.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/464105/question.mat'));
G=findgroups(stationdepth(:,1));
n=max(histcounts(G,1:max(G)+1));
out=nan(n,9);
for i=1:9
bool=(G==i);
out(1:nnz(bool),i)=stationdepth(bool,2);
end
out
out = 14×9
0 0 0 0 0 0 0 0 0 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 30 50 50 50 50 50 50 50 50 50 75 75 75 75 75 75 75 75 75 NaN NaN 100 100 100 100 100 100 100 NaN NaN 125 125 125 125 125 125 125 NaN NaN 150 150 150 150 150 150 150 NaN NaN NaN NaN 200 200 200 200 200
  1 件のコメント
Day Hong Kim
Day Hong Kim 2020 年 12 月 18 日
Thank you for the answer. I will study your code!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by