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

7 views (last 30 days)
godspeed
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 Comments
godspeed
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

Sign in to comment.

Accepted Answer

KSSV
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

More Answers (1)

Stephen23
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

Community Treasure Hunt

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

Start Hunting!