Select a row and append it at the end of a matrix | Move a selected row to end of matrix

17 ビュー (過去 30 日間)
Hello People,
Assume i have a matrix H =
[1 0 0 1
0 1 0 0
1 1 1 0
1 1 0 1
0 1 0 1
0 0 1 1]
Now I want to move row number 2 and 4 to the end of the matrix. NOT SWAP.
Like removing row 2 & row 4 and appending them after row 6. The remaining rows cascade upward in the same order.
I need pointers on how to achieve this. I unknowingly used swap and realised that it was the reason my algorithm failed. Any help would be appreciated.
Thank You

採用された回答

Matt J
Matt J 2020 年 12 月 18 日
編集済み: Matt J 2020 年 12 月 18 日
H=[1 0 0 1
0 1 0 0
1 1 1 0
1 1 0 1
0 1 0 1
0 0 1 1];
rows2move=[2,4];
idx=[setdiff(1:size(H,1), rows2move), rows2move];
H=H(idx,:)
H = 6×4
1 0 0 1 1 1 1 0 0 1 0 1 0 0 1 1 0 1 0 0 1 1 0 1
  6 件のコメント
Rishi Balasubramanian
Rishi Balasubramanian 2020 年 12 月 18 日
Yup, got the answer. Thanks a ton man.
Rishi Balasubramanian
Rishi Balasubramanian 2020 年 12 月 18 日
But what I don't understand is this. In my version, i used FIND function to find the indexes of the rows that have 1s in them.
This is your version
rows = H(:,c1)==1;
I don't understand how it works. It works. But i can't figure out how and why. Could you explain it, as I will be using many versions of this across my algorithm. It'll be really helpful.

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

その他の回答 (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