Concatenation is missing values
조회 수: 4 (최근 30일)
이전 댓글 표시
I am using the command:
doubArray = cat(1,cellStruct{:,3});
to extract an array of numeric values from a cell structure of mixed variable types. All of the variables in column 3 of the cell structure are of data type “double“.
The resulting array is missing 33 of the 116397 expected entires. The missing 33 entries are from a variety of locations within the cell structure and there does not appear to be any rhyme or reason behind which entries are missing.
Has anyone encountered this problem before and know of a solution?
댓글 수: 7
Jan
2018년 12월 6일
"The resulting array is missing 33 of the 116397 expected entires."
The cat command does not ignore values. Therefore I assume that the expectation of 116397 elements is the actual problem. Why do you assume this amount of data? How do you determine, which elements are missing? What does this reply:
S = sum(cellfun('prodofsize', cellStruct(:,3)))
This is the number of elements of all arrays contained in the 3rd column of cellStruct - by the way: isn't the part "Struct" in the name confusing here?
채택된 답변
Stephen23
2018년 12월 6일
편집: Stephen23
2018년 12월 6일
33 of the cells in the third column contain empty arrays:
>> find(cellfun('isempty',cellStruct(:,3)))
ans =
21968
21969
27848
35808
43424
52648
52649
52650
52651
52652
52653
52654
52655
60284
60285
60286
60287
60288
60289
60290
60291
60292
60293
60294
60295
60296
60297
60298
60299
65762
65763
65839
65840
While the rest of the cells in the third column apparently have one row:
>> find(cellfun('size',cellStruct(:,3),1)>1)
ans = []
In any case, the total number of rows in all of the cells in the third colum is:
>> sum(cellfun('size',cellStruct(:,3),1))
ans = 116364
And that is the exact number of rows that your numeric array contains:
>> size(doubArray,1)
ans = 116364
So far nothing suggests that any data has gone "missing" when concatenating those cell contents.
댓글 수: 2
Stephen23
2018년 12월 7일
편집: Stephen23
2018년 12월 7일
"I did not realize the concatenate function ignored empty cells"
It doesn't ignore them, they are concatenated along with everything else. You can show this quite easily using an empty array with a different number of columns, which throws an error (thus showing that the empty arrays are included in the concatenation):
>> vertcat([1,2,3],nan(0,4),[4,5,6])
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
And of course when the empty array has the same number of columns it is totally included in the output (all zero rows of it!):
>> vertcat([1,2,3],nan(0,3),[4,5,6])
ans =
1 2 3
4 5 6
Nothing "missing" and nothing "ignored".
"quite frankly I never even expected empty cells could exist in my cellStruct due to the way data is entered"
It is more reliable to check, than to just rely on what you believe is happening.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!