table with NaNs into separate cells
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
2024년 4월 24일
Thank you Voss, I now have a cell with tables. How can I pull each table that contains a specific column value. For example in the picture above, I want to pull all the tables where the 3rd column == 1 into a separate cell.
I tried but the tables combine as a double which I don't want, I want to keep the separate tables.
Can also repost as a different question.
You're welcome!
One way to have all the tables in separate cells, but only the rows where column 3 is 1, is to split them first, as in my answer, and then:
result = cellfun(@(t)t(t{:,3} == 1,:),result,'UniformOutput',false);
puccapearl
2024년 4월 24일
amazing, thank you!
Voss
2024년 4월 25일
You're welcome!
puccapearl
2024년 4월 25일
Voss, one more thing, I want to subtract the first and last values of each table in column 1,
I tried this,
F = @(x)x(end)-x(1);
b = cellfun(F, result , 'un',0);
but I need to index into the first column of each table and I'm not sure how to do this.
Voss
2024년 4월 25일
F = @(x)x{end,1}-x{1,1};
b = cellfun(F, result);
puccapearl
2024년 4월 25일
편집: puccapearl
2024년 4월 25일
ah yes I thought so! My issue is, when I do this:
result = cellfun(@(t)t(t{:,3} == 1,:),result,'UniformOutput',false);
I get a cell with some empty tables,

and that gives me an indexing error error when I try to do:
F = @(x)x{end,1}-x{1,1};
b = cellfun(F, result);
Do you know how I can remove the empty tables prior to avoid the error? I tried:
result = table2cell(rmmissing(cell2table(result)))
and
result(cellfun(@isempty,result))=[];
and
result(:,all(ismissing(result)))=[];
and
new_result = rmmissing(result);
but they do not remove the empty tables :/
This is how to do it. Try it again.
result(cellfun(@isempty,result)) = [];
puccapearl
2024년 4월 25일
It works! Thank you Voss! :D
Voss
2024년 4월 25일
You're welcome!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
