Hi
i want to check if the matrix is symmetric or not by using nested loops and display a certain message if it is or not. the problem is that it's displaying the message after comparing each element of the original matrix with the ones in the transposed or inversed matrix.. i want the message to be displayed after both matrices are compared!.
A = [ 2 6 9; 7 3 8; 10 11 12];
for i = 1:3
for j = 1:3
if(A(i,j) == A(j,i))
disp('true')
else
disp('false')
end
end
end

 採用された回答

Bruno Luong
Bruno Luong 2018 年 11 月 26 日

1 投票

disp(isequal(A,A.'))

9 件のコメント

Abdulelah  Jo
Abdulelah Jo 2018 年 11 月 26 日
it printed zeros.. what im looking for is that it prints one of the following :
disp('The matrix is symmetric!');
%or
disp('The matrix is asymmetric!');
Bruno Luong
Bruno Luong 2018 年 11 月 26 日
fprintf(['The matrix is a' repmat('\b',isequal(A,A.')) 'symmetric!\n'])
Abdulelah  Jo
Abdulelah Jo 2018 年 11 月 26 日
thank you so much.. how can i make it print the command once?
Walter Roberson
Walter Roberson 2018 年 11 月 26 日
? That already displays the output once.
Abdulelah  Jo
Abdulelah Jo 2018 年 11 月 26 日
it displays it three times! Where i should include the print statement?
Walter Roberson
Walter Roberson 2018 年 11 月 26 日
編集済み: Walter Roberson 2018 年 11 月 26 日
I suspect that you put Bruno's code into a loop. Instead it should be the entire code. It does it all including the displaying. No loop.
A = [ 2 6 9; 7 3 8; 10 11 12];
fprintf(['The matrix is a' repmat('\b',isequal(A,A.')) 'symmetric!\n']);
Abdulelah  Jo
Abdulelah Jo 2018 年 11 月 26 日
i want the matrix to be checked with nested loops.. that's what im trying.... Bruno's Answer does work but with loops it repeats the message!
Walter Roberson
Walter Roberson 2018 年 11 月 26 日
A = [ 2 6 9; 7 3 8; 10 11 12];
for i = 1 : 1
for j = 1 : 1
fprintf(['The matrix is a' repmat('\b',isequal(A,A.')) 'symmetric!\n']);
end
end
Abdulelah  Jo
Abdulelah Jo 2018 年 11 月 26 日
Thanks alot! it worked <3

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by