Conversion of multidimensional cell into string

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Guillaume
Guillaume il 23 Apr 2019
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 il 23 Apr 2019
Hello,
Thanks for replying , I want my output as
2, '010'
73 , '0111'
and so on

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 23 Apr 2019
Modificato: Stephen23 il 23 Apr 2019
>> 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 Commenti
lavanya vs
lavanya vs il 23 Apr 2019
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 il 23 Apr 2019
Modificato: Stephen23 il 23 Apr 2019
"...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'

Accedi per commentare.

Più risposte (1)

Guillaume
Guillaume il 23 Apr 2019
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)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by