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

3 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 24 Mai 2022
are you looking for one of these?:
eye(6)
ans = 6×6
1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1
1-eye(6)
ans = 6×6
0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0
godspeed
godspeed am 24 Mai 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 24 Mai 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

Weitere Antworten (1)

Stephen23
Stephen23 am 24 Mai 2022
Bearbeitet: Stephen23 am 24 Mai 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

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by