Conversion of multidimensional cell into string

1 回表示 (過去 30 日間)
lavanya vs
lavanya vs 2019 年 4 月 23 日
編集済み: Stephen23 2019 年 4 月 23 日
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
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.
lavanya vs
lavanya vs 2019 年 4 月 23 日
Hello,
Thanks for replying , I want my output as
2, '010'
73 , '0111'
and so on

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

採用された回答

Stephen23
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 件のコメント
lavanya vs
lavanya vs 2019 年 4 月 23 日
thanks your code worked , I am new to matlab if you could help me understand the code I would learn a lot.
Thank you so much though
Stephen23
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
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)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by