Scanning a matrix in zigzag order into a vector.

버전 1.0.0 (1.62 KB) 작성자: Shuvo Kumar Paul
zigzag.m takes a matrix as the only input and returns a vector containing the entries of the matrix in "ZIGZAG" fashion.
다운로드 수: 6
업데이트 날짜: 2021/7/22

라이선스 보기

function C = zigzag(A)
r = size(A,1); % number of rows of the input matrix
c = size(A,2); % number of columns of the input matrix
kk = 2;
C = [];
while kk <= r+c % the lowermost diagonal has only one element
% which has the index r by c
% scan the matrix as long as the last
% diagonal is retrieved
B = [];
% iterate through every element of the input matrix
for ii = 1:r
for jj = 1:c
if ii + jj == kk % sum of the indices in a particular diagonal
% are the same
B = [B,A(ii,jj)];
end
end
end
if mod(kk,2) == 0
C = [C,flip(B)]; % reverse the order of the diagonal entries
% evenly
else
C = [C,B];
end
kk = kk+1;
end
end

인용 양식

Shuvo Kumar Paul (2024). Scanning a matrix in zigzag order into a vector. (https://www.mathworks.com/matlabcentral/fileexchange/96229-scanning-a-matrix-in-zigzag-order-into-a-vector), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2016a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.0.0