How to cell array of string to 3D matrix?

5 ビュー (過去 30 日間)
Danupon Subanapong
Danupon Subanapong 2018 年 11 月 15 日
コメント済み: Danupon Subanapong 2018 年 11 月 15 日
I have a cell array with size 200x1 with each cell having a string array 12000x1 cell. Example of string array with size 12000x1 will be in the attached file. How can i convert this cell into a 3d numeric matrix array of size 12000x5x200 ?? Thank you in advance.
  2 件のコメント
Stephen23
Stephen23 2018 年 11 月 15 日
編集済み: Stephen23 2018 年 11 月 15 日
"I have a cell array with size 200x1 with each cell having a string array 12000x1 cell"
But what you uploaded is a 12000x1 cell array, where each cell contains a character vector.:
>> size(AB)
ans = 12000 1
>> all(cellfun('isclass',AB,'char'))
ans = 1
Your question and your supplied data do not match.
Danupon Subanapong
Danupon Subanapong 2018 年 11 月 15 日
I am sorry. please refer to the new attached file.

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

採用された回答

Stephen23
Stephen23 2018 年 11 月 15 日
編集済み: Stephen23 2018 年 11 月 15 日
This converts your supplied cell array (attached) into a numeric array:
>> C = cellfun(@(s)sscanf(s,'%f'),AB,'uni',0);
>> A = cat(3,C{:});
>> size(A)
ans =
5 1 12000
Note that your data description does not match the data in your .mat file.
  2 件のコメント
Stephen23
Stephen23 2018 年 11 月 15 日
編集済み: Stephen23 2018 年 11 月 15 日
Two methods that work with the data in your second example file.
Method one: cellfun and cellmat:
>> load Example_of_cell2.mat
>> F = @(s) sscanf(s,'%f',[1,Inf]);
>> A = cell2mat(cellfun(F,cat(3,BB{:}),'uni',0));
>> size(A)
ans =
12000 5 200
Method two: sprintf, reshape and permute:
>> C = [BB{:}];
>> B = sscanf(sprintf('%s\v',C{:}),'%f');
>> B = permute(reshape(B,[5,12000,200]),[2,1,3]);
>> size(B)
ans =
12000 5 200
Method two is quite possibly faster. Comparing the outputs:
>> isequal(A,B)
ans = 1
Danupon Subanapong
Danupon Subanapong 2018 年 11 月 15 日
It works. Thank you so much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by