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

12 vues (au cours des 30 derniers jours)
Sarah A
Sarah A le 17 Jan 2020
Commenté : Sarah A le 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,

Réponse acceptée

David Hill
David Hill le 17 Jan 2020
a=cell(1,164);
for j=1:164
a{j}=[Key{j,1:end}]-48;
end
  3 commentaires
dpb
dpb le 17 Jan 2020
Modifié(e) : dpb le 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)).';
Sarah A
Sarah A le 18 Jan 2020
Thank you all :)

Connectez-vous pour commenter.

Plus de réponses (2)

Adam Danz
Adam Danz le 17 Jan 2020
Modifié(e) : Adam Danz le 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 commentaires
dpb
dpb le 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...
Sarah A
Sarah A le 18 Jan 2020
the output in keyrows. thank you.

Connectez-vous pour commenter.


dpb
dpb le 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...

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by