MATLAB Answers

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

3 views (last 30 days)
Sarah A
Sarah A on 17 Jan 2020
Commented: Sarah A on 18 Jan 2020
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

Sign in to comment.

Accepted Answer

David Hill
David Hill on 17 Jan 2020
a=cell(1,164);
for j=1:164
a{j}=[Key{j,1:end}]-48;
end

  3 Comments

Adam Danz
Adam Danz on 17 Jan 2020
I like this. It can also be reduced to 1 line without relying on knowing the size of Key.
a = arrayfun(@(row){[Key{row,:}]-48},1:size(Key,1));
dpb
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)).';

Sign in to comment.

More Answers (2)

Adam Danz
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
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...

Sign in to comment.


dpb
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

Sign in to comment.

Sign in to answer this question.


Translated by