Conversion of multidimensional cell into string
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
lavanya vs
el 23 de Abr. de 2019
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 comentarios
Guillaume
el 23 de Abr. de 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.
Respuesta aceptada
Stephen23
el 23 de Abr. de 2019
Editada: Stephen23
el 23 de Abr. de 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 comentarios
Stephen23
el 23 de Abr. de 2019
Editada: Stephen23
el 23 de Abr. de 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'
Más respuestas (1)
Guillaume
el 23 de Abr. de 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)
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!