Cell Array to Matrix but keep zeros

8 ビュー (過去 30 日間)
Aaron
Aaron 2017 年 3 月 3 日
コメント済み: Aaron 2017 年 3 月 3 日
Hello! So my problem is that I have a tridiagonal cell array, say
x y z [] [] []
[] x y z [] []
[] [] x y z []
[] [] [] x y z
Where each tridiagonal element (x, y, and z) is a 2x2 matrix. I want to use the cell2mat function to create a matrix and make each cell array [] element a 2x2 matrix of zeros (so [] -> [0 0;0 0]). So I would end up going from a NxN cell array to a 4Nx4N matrix. When I use cell2mat, however, the []'s are removed and the matrix becomes disordered. Could anyone suggest a tip to make this conversion possible? Thank you! (and thanks for reading, either way)

採用された回答

Stephen23
Stephen23 2017 年 3 月 3 日
編集済み: Stephen23 2017 年 3 月 3 日
Where C is your cell array:
C(cellfun('isempty',C)) = {[0,0;0,0]};
out = cell2mat(C);
Here is a complete working simplified example:
>> C = {[1,2;3,4],[];[],[5,6;7,8]};
>> C(cellfun('isempty',C)) = {[0,0;0,0]};
>> cell2mat(C)
ans =
1 2 0 0
3 4 0 0
0 0 5 6
0 0 7 8
  1 件のコメント
Aaron
Aaron 2017 年 3 月 3 日
Perfect! Thank you very much! :D

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by