Conversion of multidimensional cell into string
1 次查看(过去 30 天)
显示 更早的评论
2 [0,1,0]
73 [0,1,1,1]
97 1
108 [0,1,1,0]
109 [0,0,0,1]
110 [0,0,0,0]
118 [0,0,1,1]
121 [0,0,1,0]
This is a cell I want the array as 010
0111
1
and so on that is convert it into char for binary purpose
2 个评论
Guillaume
2019-4-23
Can you please use valid matlab syntax to describe your input and wanted output?
I think your input might be:
C = {2, [0,1,0];
73, [0,1,1,1];
97, 1;
108, [0,1,1,0];
109, [0,0,0,1];
110, [0,0,0,0];
118, [0,0,1,1];
121, [0,0,1,0]} %This is valid matlab syntax. We can paste this straight into matlab
I have no idea what output you want.
采纳的回答
Stephen23
2019-4-23
编辑:Stephen23
2019-4-23
>> C(:,2) = cellfun(@(v)char(v+'0'),C(:,2),'uni',0)
C =
2 '010'
73 '0111'
97 '1'
108 '0110'
109 '0001'
110 '0000'
118 '0011'
121 '0010'
2 个评论
Stephen23
2019-4-23
编辑:Stephen23
2019-4-23
"...if you could help me understand the code I would learn a lot."
I used cellfun to apply a function to each vector:
C(:,2) = cellfun(@(v)char(v+'0'),C(:,2),'uni',0) % my answer, broken down:
% @(v)char(v+'0') % anonymous function [v] -> 'v'.
% C(:,2) % get second column of cell array.
% cellfun( , ,'uni',0) % iterate over each vector (cell content).
%C(:,2) = % allocate to second column of cell array.
Learn more about cellfun and anonymous functions:
The conversion from binary vector to character is by simply adding the character value for '0' (which is 48) onto the binary values and then converting to character. This works for any digits:
>> char([1,2,5,9]+'0')
ans = '1259'
>> char([1,2,5,9]+48) % equivalent
ans = '1259'
更多回答(1 个)
Guillaume
2019-4-23
Taking a guess here, since you're still not using matlab syntax for your example:
C = {2, [0,1,0];
73, [0,1,1,1];
97, 1;
108, [0,1,1,0];
109, [0,0,0,1];
110, [0,0,0,0];
118, [0,0,1,1];
121, [0,0,1,0]} %demo data
C(:, 2) = cellfun(@(v) char(v + '0'), C(:, 2), 'UniformOutput', false)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!