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 ?

采纳的回答

Walter Roberson
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 个评论
Alberto Acri
Alberto Acri 2023-7-20
Thank you for your reply @Walter Roberson. I am not clear on the difference of the two examples shown. Does the second example refer in the case where line 5 of test_p contains all 0x0 double elements?
Walter Roberson
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 个)

Matt J
Matt J 2023-7-20
x=zeros(0,0);
y=uint8( reshape(x,0,1) );
whos x y
Name Size Bytes Class Attributes x 0x0 0 double y 0x1 0 uint8

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by