transform an empty matrix '0x0 double' into a matrix '0x1 uint8'
11 次查看(过去 30 天)
显示 更早的评论
Hello! How can I transform an empty matrix '0x0 double' into a matrix '0x1 uint8' as in the figure?
For example in my case, I have a cell 'test_p'. Rows 3 and 4 (0x0 double) should become like rows 1,2 and 5 (0x1 uint8).
test_p = importdata("test_p.mat");
M_0x1_uint8 = test_p{1, 1};
M_0x0_double = test_p{3, 1};
The uint8 command should do the trick. How can I apply it only to those '0x0 double' arrays ?
0 个评论
采纳的回答
Walter Roberson
2023-7-20
mask = cellfun(@isempty, test_p);
test_p(mask) = {zeros(0,1,'uin8')};
Unless, that is, you might have entries that are (for example) '' (the empty character vector). In such a case
mask = cellfun(@(C) isdouble(C) && isempty(C), test_p);
test_p(mask) = {zeros(0,1,'uin8')};
2 个评论
Walter Roberson
2023-7-20
The first version of the code looks inside the cell array test_p and finds all of the entries that are "empty" and changes them all to 0 x 1 uint8.
The second version of the code looks inside the cell array test_p and finds all of the double precision entries that are "empty" and changes them all to 0 x 1 uint8.
The difference is that hypothetically your cell array might contain empty entries that are not double precision and that you might not want converted. For example it might contain empty character vectors or empty figure() handles that you want to leave alone.
There is another possibility that this code does not account for in the form posted: you might potentially have some double precision empty arrays that are not 0 x 0 that you do not want converted. For example you could theoretically have an 768 x 1024 x 3 x 0 array that you did not want converted.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!