Add all subsequent rows with selected row
    14 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Rishi Balasubramanian
 el 29 de En. de 2021
  
    
    
    
    
    Respondida: Anmol Dhiman
    
 el 2 de Feb. de 2021
            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?
0 comentarios
Respuesta aceptada
  Anmol Dhiman
    
 el 2 de Feb. de 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
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 
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Matrix Indexing en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

