How to read a Matrix from the top left corner to the bottom right corner in a diagonal fashion
3 views (last 30 days)
Show older comments
Given the matrix as shown I want to be able to read it diagonally putting the given numbers into a vector. For example, Read 1,2,5,3,6,3 etc converting this entire matrix into the vector 1,2,5,3,6,3,4,7,2,9,8,5,7,4,5,4. I want to write a code to be able to do that for a matrix of any size. I am assuming some type of loop but not 100% sure.

1 Comment
James Tursa
on 22 Jan 2018
Edited: James Tursa
on 22 Jan 2018
What have you done so far? What specific problems are you having with your code?
Answers (1)
Star Strider
on 22 Jan 2018
This works:
M = [1 2 3 4; 5 6 7 8; 3 2 5 4; 9 7 5 4];
Mf = flip(M,1);
ref = size(M,1);
for k1 = 1:2*ref-1
dv = diag(Mf, k1-ref);
V{k1} = flip(dv);
end
Out = cell2mat(V')
0 Comments
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!