How to convert each row in a cell array to a (1*n ) double matrix?

5 ビュー (過去 30 日間)
Sarah A
Sarah A 2020 年 1 月 17 日
コメント済み: Sarah A 2020 年 1 月 18 日
Hello,
I have the attached cell array of size (164*102) where each cell is either contains 4-bits binary string or nothing (empty). How can I convert each row in the cell array to a seperate double matrix where each bit in the 4-bits string in a cell and the empty cell deleted.
For example,
if in Key array matrix we have the following row:
['0011', [ ], '1010', [ ], [ ], [ ], '0000', '1111']
then we convert it a seperate 1*16 double matrix:
[0,0,1,1,1,0,1,0,0,0,0,0,1,1,1,1].
Regrads,

採用された回答

David Hill
David Hill 2020 年 1 月 17 日
a=cell(1,164);
for j=1:164
a{j}=[Key{j,1:end}]-48;
end
  3 件のコメント
dpb
dpb 2020 年 1 月 17 日
編集済み: dpb 2020 年 1 月 17 日
Good catch of converting to arrayfun with the row index instead of trying to operate directly on the initial cell array. I had assumed a fixed number of non-empty cells per row as well; that turns out not to be the case in the actual data attached as well.
Also good recognition that the empty cells will be silently ignored so don't have to explicitly remove them.
Slight refinement for legibility to subtract the '0' instead of more cryptic (imo, anyway) '48' and return column-oriented cell array to match rows would be--
a = arrayfun(@(row){[Key{row,:}]-'0'},1:size(Key,1)).';
Sarah A
Sarah A 2020 年 1 月 18 日
Thank you all :)

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

その他の回答 (2 件)

Adam Danz
Adam Danz 2020 年 1 月 17 日
編集済み: Adam Danz 2020 年 1 月 17 日
I like David Hill's solution but here's another one because I already wrote it up so, why not share it?
load('Key.mat')
% Insert empty char arrays in empty cells
Key(cellfun(@isempty,Key)) = {''};
% Convert char ('1111') to numeric vector ([1 1 1 1])
KeyVectors = cellfun(@(c){str2double(cellstr(c.').')},Key);
% Combine each row of cells
KeyRows = arrayfun(@(row){[KeyVectors{row,:}]},1:size(KeyVectors,1)).';
% Remove the NaN place holders that mark cells that were originally empty
KeyRows = cellfun(@(c){c(~isnan(c))},KeyRows);
Note that this matches David's solution other than my result is a column array and his is a row array.
isequal(KeyRows, a') % = true
  2 件のコメント
dpb
dpb 2020 年 1 月 17 日
"Note that this matches David's solution other than my result in a column array and his is a row array. "
I suspect that's just an oversight on David's part in the preallocation step of reversing row, column position relative to the original size of input array...
Sarah A
Sarah A 2020 年 1 月 18 日
the output in keyrows. thank you.

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


dpb
dpb 2020 年 1 月 17 日
Use the dual representation of char() data as numeric that MATLAB will cast silently...
c=[{'0011'}, {[ ]}, {'1010'}, {[ ]}, {[ ]}, {[ ]}, {'0000'}, {'1111'}]; % example input
c=c(~cellfun(@isempty,c)); % keep non-empty cells only
m=strcat(cc{:})-'0'; % convert to numeric array
Result:
>> m =
0 0 1 1 1 0 1 0 0 0 0 0 1 1 1 1
>>
OTOMH, looks to me like you'll need to loop over the rows of the original array; don't see trivial way to vectorize this by row altho didn't work on it much...

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by