How to convert each row in a cell array to a (1*n ) double matrix?
4 views (last 30 days)
Show older comments
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,
0 Comments
Accepted Answer
David Hill
on 17 Jan 2020
a=cell(1,164);
for j=1:164
a{j}=[Key{j,1:end}]-48;
end
3 Comments
dpb
on 17 Jan 2020
Edited: dpb
on 17 Jan 2020
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)).';
More Answers (2)
Adam Danz
on 17 Jan 2020
Edited: Adam Danz
on 17 Jan 2020
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 Comments
dpb
on 17 Jan 2020
"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...
dpb
on 17 Jan 2020
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...
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!