필터 지우기
필터 지우기

Move multiple rows without swapping

조회 수: 2 (최근 30일)
Rishi Balasubramanian
Rishi Balasubramanian 2020년 12월 23일
댓글: Rishi Balasubramanian 2020년 12월 23일
Hello,
I have a mxn matrix of binary data. I want to write a code where I find the rows in column n which have 1s in them and move them to bottom of the row. I want a generalised code for this as it will be in a loop where multiple iterations of this will be executed.
%For example, assume this is my matrix
% a b c d e f g h i j k l
H = [0 0 1 1 0 1 0 1 0 1 1 0; %1
0 0 0 1 0 1 1 0 1 0 1 0; %2
0 1 1 0 0 1 1 1 0 0 0 1; %3
0 1 1 0 1 0 1 0 1 0 0 1; %4
0 0 0 1 1 0 0 1 1 0 0 0; %5
0 1 0 0 1 0 0 0 0 0 1 0]; %6
%In column 'l' there are 2 rows with 1s in them (row 3 and 4).
% I want to move rows 3 and 4 to the bottom. My final matrix should look like this.
% a b c d e f g h i j k l
H = [0 0 1 1 0 1 0 1 0 1 1 0; %1
0 0 0 1 0 1 1 0 1 0 1 0; %2
0 0 0 1 1 0 0 1 1 0 0 0; %5
0 1 0 0 1 0 0 0 0 0 1 0; %6
0 1 1 0 0 1 1 1 0 0 0 1; %3
0 1 1 0 1 0 1 0 1 0 0 1]; %4
%Any help is appreciated. I thank in advance.

채택된 답변

Ive J
Ive J 2020년 12월 23일
H = randi([0 1], 6, 10)
H =
0 1 0 1 1 0 0 0 1 0
1 1 1 0 1 1 0 1 1 1
0 0 0 0 1 0 0 1 0 1
1 1 0 0 1 1 1 0 0 1
1 1 0 0 1 1 1 1 0 0
1 0 1 1 1 1 1 0 1 1
col = 9;
H = [H(H(:, col) < 1, :); H(H(:, col) > 0, :)]
0 0 0 0 1 0 0 1 0 1
1 1 0 0 1 1 1 0 0 1
1 1 0 0 1 1 1 1 0 0
0 1 0 1 1 0 0 0 1 0
1 1 1 0 1 1 0 1 1 1
1 0 1 1 1 1 1 0 1 1
  댓글 수: 5
Rishi Balasubramanian
Rishi Balasubramanian 2020년 12월 23일
Wonderful, that works perfectly. Thanks man.
Rishi Balasubramanian
Rishi Balasubramanian 2020년 12월 23일
Hello, One more question. Hopefully its the last. Say I want to move a single row to a specific postion? How do I do that?
For ex
% a b c d e f g h i j k l
H = [0 0 1 1 0 1 0 1 0 1 1 0; %1
0 0 0 1 0 1 1 0 1 0 1 0; %2
0 1 1 0 0 1 1 1 0 0 0 1; %3
0 1 1 0 1 0 1 0 1 0 0 1; %4
0 0 0 1 1 0 0 1 1 0 0 0; %5
0 1 0 0 1 0 0 0 0 0 1 0]; %6
% I want to move row 2 to row index 5 such that my final matrix is -
% a b c d e f g h i j k l
H = [0 0 1 1 0 1 0 1 0 1 1 0; %1
0 1 1 0 0 1 1 1 0 0 0 1; %3
0 1 1 0 1 0 1 0 1 0 0 1; %4
0 0 0 1 1 0 0 1 1 0 0 0; %5
0 0 0 1 0 1 1 0 1 0 1 0; %2
0 1 0 0 1 0 0 0 0 0 1 0]; %6

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by