Extract arrays of a matrix using FOR loop

9 visualizaciones (últimos 30 días)
AliAbKo
AliAbKo el 19 de En. de 2023
Respondida: AKAYDIN el 19 de En. de 2023
Hello everyone
the question is: Write a code using FOR loop that extract Main diagonal of any n*n matrix.
i know diag will do the job but i wanna write it with FOR loop
Thanks in advance

Respuestas (1)

AKAYDIN
AKAYDIN el 19 de En. de 2023
A = rand(10,10) % random matrix when n=10
A = 10×10
0.8978 0.6904 0.2470 0.1673 0.0527 0.2011 0.7330 0.9430 0.0088 0.3763 0.0042 0.5003 0.2743 0.2955 0.7764 0.4320 0.6582 0.3441 0.8988 0.9263 0.9867 0.2533 0.0639 0.8561 0.1614 0.5897 0.7317 0.7973 0.9941 0.6897 0.2935 0.3559 0.7120 0.3738 0.0453 0.6130 0.1864 0.2341 0.3009 0.1276 0.2284 0.4807 0.9728 0.8918 0.2230 0.4444 0.0003 0.4556 0.6955 0.6698 0.9492 0.5336 0.1320 0.4075 0.0789 0.4972 0.6743 0.0642 0.9191 0.3812 0.7728 0.5496 0.9692 0.0059 0.6290 0.0335 0.1770 0.5867 0.7811 0.1029 0.5000 0.7460 0.1441 0.0938 0.4036 0.9811 0.7278 0.6073 0.1062 0.7536 0.6630 0.3329 0.6754 0.2912 0.2135 0.0484 0.0749 0.5416 0.3906 0.1092 0.5241 0.8132 0.0720 0.7198 0.6078 0.3771 0.5940 0.5341 0.6533 0.5983
for i=1:length(A)
diagonal(i) = A(i,i);
end
diagonal
diagonal = 1×10
0.8978 0.5003 0.0639 0.3738 0.2230 0.4972 0.1770 0.6073 0.3906 0.5983

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by