필터 지우기
필터 지우기

table with NaNs into separate cells

조회 수: 3 (최근 30일)
puccapearl
puccapearl 2024년 4월 24일
댓글: Voss 2024년 4월 25일
I have a table (M) with 4 columns (columns named X, Y, Z,K) , there is a break between the data that loads as NaN.
I want place each data chunk into cells with all 4 columns (X, Y, Z,K).
Thank you!

채택된 답변

Voss
Voss 2024년 4월 24일
% making a table with some all-NaN rows:
Var = randi(100,12,4);
Var([3 9],:) = NaN;
M = array2table(Var)
M = 12x4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 7 12 6 2 80 39 44 44 NaN NaN NaN NaN 48 78 45 83 3 97 37 62 35 55 21 4 37 44 22 79 85 95 92 85 NaN NaN NaN NaN 98 21 58 64 13 57 8 58 96 89 36 89
% split the table on the all-NaN rows into a cell array of tables:
idx = find(all(isnan(M{:,:}),2));
s_idx = [1; idx+1];
e_idx = [idx-1; size(M,1)];
result = arrayfun(@(s,e)M(s:e,:),s_idx,e_idx,'UniformOutput',false)
result = 3x1 cell array
{2x4 table} {5x4 table} {3x4 table}
celldisp(result)
result{1} = Var1 Var2 Var3 Var4 ____ ____ ____ ____ 7 12 6 2 80 39 44 44 result{2} = Var1 Var2 Var3 Var4 ____ ____ ____ ____ 48 78 45 83 3 97 37 62 35 55 21 4 37 44 22 79 85 95 92 85 result{3} = Var1 Var2 Var3 Var4 ____ ____ ____ ____ 98 21 58 64 13 57 8 58 96 89 36 89
  댓글 수: 10
puccapearl
puccapearl 2024년 4월 25일
It works! Thank you Voss! :D
Voss
Voss 2024년 4월 25일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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