Divide 1D Array into a 2D Array by finding a flag value

조회 수: 1 (최근 30일)
Hussam Ibrahim
Hussam Ibrahim 2017년 11월 22일
답변: KSSV 2017년 11월 22일
Hello,
I have a LabVIEW code where I am accquiring timestamps for scan lines, but at the end of each scan line, I have labview add a flag number into the array so I know where each scan line ends. I am trying to write a Matlab code, where I divide this 1D array to a 2D array (each row is the timestamps of 1 scan lines).
e.g. 1D Array = [10 11 12 13 14 15 18 19 0 21 23 24 25 26 27 28 29 0 31 34 35 36 36 38 39 40 0] here 0 is the flag. I want this to become a 2D array that is 2D Array = [10 11 12 13 14 15 18 19; 21 23 24 25 26 27 28 29; 31 34 35 36 36 38 39 40].
Best,

답변 (1개)

KSSV
KSSV 2017년 11월 22일
A = [10 11 12 13 14 15 18 19 0 21 23 24 25 26 27 28 29 0 31 34 35 36 36 38 39 40 0] ;
B = [10 11 12 13 14 15 18 19; 21 23 24 25 26 27 28 29; 31 34 35 36 36 38 39 40] ;
flag = 0 ;
idx = find(A==flag) ;
L = unique(diff(idx)-1) ; % number of columns
% remove flags
A(A==0) = [] ;
% reshape to 2D array
iwant = reshape(A,[],L)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by