# In a 6x6 square matrix A, how to select elements in i-th row with A(i,j) ruled out

9 views (last 30 days)
godspeed on 24 May 2022
Edited: Stephen23 on 24 May 2022
n=12;
A=zeros(n,n);
B=zeros(n,n);
if A(i,(i-1):(i+1):n)==0
B(i,i)=1;
end
##### 2 CommentsShowHide 1 older comment
godspeed on 24 May 2022
Forgive me as the question was confusing. The question should be: say A is 4x4 square matrix and B=zeros(4,4). If the i-th diagonal element of A is 1 and all the non-diagonal elements are 0 in that i-th row, then B(i,i)=1. In this case, B(3,3)=1 and all other elements should be 0.
A =
0 0 -1 1
1 1 0 0
0 0 1 0
1 -1 0 1

KSSV on 24 May 2022
A = [0 0 -1 1
1 1 0 0
0 0 1 0
1 -1 0 1] ;
B = zeros(size(A)) ;
for i = 1:4
if A(i,i) == 1 % if disagonal element = 1
temp = A(i,:) ; % get the ith row
temp(i) = [] ; % remove the diagonal element
if ~any(temp) % check whether all are zeros
B(i,i) = 1 ; % Make B(i,i) = 1
end
end
end
B
B = 4×4
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
godspeed on 24 May 2022
Thank you so much.

Stephen23 on 24 May 2022
Edited: Stephen23 on 24 May 2022
A = [0,0,-1,1;1,1,0,0;0,0,1,0;1,-1,0,1]
A = 4×4
0 0 -1 1 1 1 0 0 0 0 1 0 1 -1 0 1
B = +diag(diag(A) & ~any(A-eye(size(A)),2))
B = 4×4
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0