Clear Filters
Clear Filters

how to convert a given matrix in an array while reading all the elements of matrix in a particular order.

1 view (last 30 days)
I want to write a program in matlab to convert a matrix into an array. the values of the matrix should be read diagionally starting from any one element of the matrix then forming a continuous loop till end
For example;
let's assume the matrix
[1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24]
the matrix converted into an array by reading the elements diagonally like
1 7 2 3 8 13 19 14 9 4 5 10 15 20 21 16 11 6 12 17 22 23 18 24
Please help!!

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jul 2015
  3 Comments

Sign in to comment.

More Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 18 Jul 2015
A=[1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18; 19 20 21 22 23 24]
q=spdiags(rot90(A))
q(:,2:2:end)=flipud(q(:,2:2:end))
out=nonzeros(q(:))'

Andrei Bobrov
Andrei Bobrov on 27 Jul 2016
Variant by Azzi with the small addition:
A = randi([-6 6],4,6);
ii = reshape(1:numel(A),size(A));
z = spdiags(ii(end:-1:1,:));
z(:,1:2:end) = z(end:-1:1,1:2:end);
out = A(z(z~=0))';

Categories

Find more on Data Type Conversion 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!