How can I extract the numbers divisible by 3 from the diagonal of a nxn matrix composed of random integers?
7 次查看(过去 30 天)
显示 更早的评论
I can create a random matrix of nxn with integers in [100;1000] interval, for example:
n=input('Choose the number of lines and columns of the square matrix: ')
A=randi([100,1000], n, n)
D=diag(A)
So the numbers in D are the numbers from the main diagonal of A. Now I would like to extract the numbers in D that are divisible by 3 and create matrix with those numbers, how can i do this?
0 个评论
采纳的回答
KALYAN ACHARJYA
2022-10-27
n=input('Choose the number of lines and columns of the square matrix: ')
A=randi([100,1000], n, n)
D=diag(A)
mat=D(mod(D,3)==0)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!