How can I extract the numbers divisible by 3 from the diagonal of a nxn matrix composed of random integers?

3 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

KALYAN ACHARJYA
KALYAN ACHARJYA am 27 Okt. 2022
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)

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by