Add all subsequent rows with selected row

4 views (last 30 days)
Hello,
I have a mxn binary matrix
H = [1 1 1 0 1 1 1;
0 1 0 0 1 0 1;
1 1 0 1 0 1 1;
1 1 1 0 0 0 0;
0 0 0 0 1 0 0;
0 0 0 0 1 1 1;
1 0 0 1 0 1 0];
My goal is to convert this matrix into an Upper Triangular Matrix using Row/column operations and XOR operations only.
My Algorithm for that is
for Row = r1 and column = c1
check, if row, column is 1
if check = 1
select all subsequent rows with 1s in same column (except seleted row and previous rows)
else
find index of the first 1 in the selected row
move that column to (row, column) position
select all subsequent rows with 1s in same column (except seleted row and previous rows)
end
then Add rows all rows below r1 (subsequent) with r1 and replace them
r1 = r1+1;
c1 = c1+1;
end
This is what I currently have
%dm = Last Row of Matrix Mat
%dr = begining row
%dc1 = begining column
for j = 1:dm
chk = find(Mat(dr, dc1)); %see if 1st element of row 1 == 1;
if (chk == 1)
mark = find(Mat(1:end, dc1) == 1);
mark(mark(dr)==[];
else
colo = find(Mat(dr, dc1:end), 1, 'first'); %find column with first 1 in first row
Mat = [Mat(:, 1:dc1-1) colo Mat(:, dc1:end)];
mark = find(Mat(dr:end, dc1) == 1);
mark(mark(dr))=[];
end
for i = 1:length(mark)
Mat(mark, :) = xor(Mat(mark, :), Mat(dr, :));
end
dr = dr+1;
dc1 = dc1+1;
end
Since dr (curret row index) keeps changing on every loop, I am failing in my task.
The unexpected output of my Program - It is unable to select subsequent rows only. If row 3 is selected, then the rows to be added start from after 3 (4, 5, 6 and so on). But my program chooses the previous rows too. How do I change this to my desired method?

Accepted Answer

Anmol Dhiman
Anmol Dhiman on 2 Feb 2021
Hi Rishi,
Based on the information provided there are two issues that you are facing
1) How to add subsequent rows with a selected row
I suggest you to follow approch mentioned in this answer.
2) How to select rows values greater than current row (dr)
You can use
mark(mark<dr)=[];
This will ensure all the rows which are less than current row are removed.
Hope it helps

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by