Filter löschen
Filter löschen

transform an empty matrix '0x0 double' into a matrix '0x1 uint8'

11 Ansichten (letzte 30 Tage)
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 ?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Jul. 2023
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 Kommentare
Alberto Acri
Alberto Acri am 20 Jul. 2023
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 am 20 Jul. 2023
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 20 Jul. 2023
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

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by