transform an empty matrix '0x0 double' into a matrix '0x2 double'

12 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 7 月 20 日
コメント済み: Bruno Luong 2023 年 7 月 20 日
Hello! How can I transform an empty matrix '0x0 double' into a matrix '0x2 double' as in the figure?
For example in my case, I have a cell 'test_p'. Rows 3,4,5 (0x0 double) should become like rows 1 and 2 (0x2 double).
test_p = importdata("test_p.mat");
data_0x2_double = test_p{1, 1};
data_0x0_double = test_p{3, 1};

採用された回答

Bruno Luong
Bruno Luong 2023 年 7 月 20 日
編集済み: Bruno Luong 2023 年 7 月 20 日
test_p = importdata("test_p.mat")
test_p = 5×1 cell array
{0×2 double} {0×2 double} {0×0 double} {0×0 double} {0×0 double}
ir = cellfun('size',test_p,1)==0 & cellfun('size',test_p,2)==0
ir = 5×1 logical array
0 0 1 1 1
test_p(ir) = {zeros(0,2)}
test_p = 5×1 cell array
{0×2 double} {0×2 double} {0×2 double} {0×2 double} {0×2 double}

その他の回答 (2 件)

Steven Lord
Steven Lord 2023 年 7 月 20 日
Since a 0-by-0 array and a 0-by-2 array have the same number of elements you can reshape one into the other.
A = zeros(0, 0)
A = []
B = reshape(A, [0 2])
B = 0×2 empty double matrix
C = reshape(B, 0, 0)
C = []
whos A B C
Name Size Bytes Class Attributes A 0x0 0 double B 0x2 0 double C 0x0 0 double
  3 件のコメント
Steven Lord
Steven Lord 2023 年 7 月 20 日
Your code below made an assumption about the user's data, one that's likely valid (and is valid WRT the original question) but in some cases may not be.
A = zeros(0, 0, 'single');
B = reshape(A, [0 2]);
C = zeros(0, 2);
whos A B C
Name Size Bytes Class Attributes A 0x0 0 single B 0x2 0 single C 0x2 0 double
B and C are different classes. You could modify the call that creates C to always create it 'like' A or with the same type as A, but reshape will not change the type and is shorter than either of the calls that create C1 or C2 below.
C1 = zeros(0, 2, 'like', A)
C1 = 0×2 empty single matrix
C2 = zeros(0, 2, class(A))
C2 = 0×2 empty single matrix
Bruno Luong
Bruno Luong 2023 年 7 月 20 日
OK in short you want to preserve the class of the data with reshape.

サインインしてコメントする。


Adam Danz
Adam Danz 2023 年 7 月 20 日
編集済み: Adam Danz 2023 年 7 月 20 日
To create an empty double with a specified size,
data_0x2_double = zeros(0,2)
data_0x2_double = 0×2 empty double matrix
Or,
data_0x2_double = double.empty(0,2)
data_0x2_double = 0×2 empty double matrix
See also Steve Lord's solution using reshape.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by