How to replace the empty cells in a cell array by a 4-bits string?
9 ビュー (過去 30 日間)
古いコメントを表示
Hello,
How to replace the empty cells " [ ] " in the attached matrix to a 4-bits string we can convert the whole array matrix to double.
For example,
if we have thw following row:
[ '0001', '0101' , '1010', [ ], [ ], [ ], '1111']
it becomes:
[0,0,0,1,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1]
0 件のコメント
採用された回答
Akira Agata
2020 年 1 月 19 日
How about the following?
load('Key.mat');
idx = cellfun(@isempty,Key); % Find the indexes of empty cell
Key(idx) = {'0000'}; % Replace the empty cells with '0000'
3 件のコメント
Thomas Carpenter
2024 年 11 月 28 日
編集済み: Thomas Carpenter
2024 年 11 月 28 日
Using the optimised 'length' operation for cellfun - i.e ~cellfun('length',Key) - is generally more efficient than calling @isempty.
Walter Roberson
2024 年 11 月 28 日
Timing test:
Key = num2cell(rand(100,100));
whos Key
Key(randi(100*100,50,1)) = {[]};
t1 = timeit(@() cellfun(@isempty,Key), 1)
t2 = timeit(@() ~cellfun('length',Key), 1)
So the 'length' version is indeed faster.
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!