matrix manupulation and reading diagonally
Afficher commentaires plus anciens
Hello Team,
i have a matrix
Matrix_test = [13,10,5,2;7,14,11,6;3,8,15,12;1,4,9,16];

I want to read the matrix in the order mentioned in the matrix.
Starting from left bottom (1), then right top (2) and then again 2nd diagonal from left (3,4) and then 2nd right diagonal (5,6) and so on and so forth.
Please support in resolving it.
Edit : i there any way to create this kind of matrix for any n-by-n matrix automatically. i have created this Matrix_test manually but if i want 8x8 matrix in similar pattern . How to create it through generic code.
Thanks in advance.
Réponse acceptée
Plus de réponses (1)
Bruno Luong
le 8 Déc 2020
Modifié(e) : Bruno Luong
le 8 Déc 2020
Matrix_test = [13,10,5,2;7,14,11,6;3,8,15,12;1,4,9,16]
[m,n]=size(Matrix_test);
[i,j]=ndgrid(1:m,1:n);
jmi = j(:)-i(:);
[~,is]=sortrows([-abs(jmi) jmi]);
Matrix_test(is)
4 commentaires
NIKHIL
le 11 Déc 2020
NIKHIL
le 11 Déc 2020
Bruno Luong
le 11 Déc 2020
Create matrix with dimension m x n.
m=8; n=8;
[i,j]=ndgrid(1:m,1:n);
jmi = j(:)-i(:);
[~,is]=sortrows([-abs(jmi) jmi]);
M=zeros(m,n);
M(is)=1:m*n
NIKHIL
le 11 Déc 2020
Catégories
En savoir plus sur Create Requirement Links dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!