How to convert each row in a cell array to a (1*n ) double matrix?
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sarah A
el 17 de En. de 2020
Comentada: Sarah A
el 18 de En. de 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 comentarios
Respuesta aceptada
David Hill
el 17 de En. de 2020
a=cell(1,164);
for j=1:164
a{j}=[Key{j,1:end}]-48;
end
3 comentarios
dpb
el 17 de En. de 2020
Editada: dpb
el 17 de En. de 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)).';
Más respuestas (2)
Adam Danz
el 17 de En. de 2020
Editada: Adam Danz
el 17 de En. de 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 comentarios
dpb
el 17 de En. de 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
el 17 de En. de 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 comentarios
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!