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

조회 수: 4 (최근 30일)
Alberto Acri
Alberto Acri 2023년 7월 20일
댓글: Walter Roberson 2023년 7월 20일
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에서 Numeric Types에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by