How to split binary cells and convert them to dec?

2 次查看(过去 30 天)
I have a cell array consisting of binary 100x1 e.g
{'0100101000101000'}
{'0100101000101000'}
{'0100101000101000'}
...
{'0100101000101000'}
How do i take the first 3 binary from each cell and then convert it into dec?
maybe a cellfun can be helpful here but i do get some errors
I know you can use bin2dec i should expect a 2 for every cell.
Thanks!

采纳的回答

zhehao.nkd
zhehao.nkd 2022-1-25
C{1} = {'0100101000101000'};
C{2} = {'0100101000101000'};
C{3} = {'0100101000101000'};
if C is your original cell, then:
cellfun(@(x) bin2dec(x{1}(1:3)),C)
ans = 1×3
2 2 2
Is that what you need?
  1 个评论
MKM
MKM 2022-1-25
Perfect, I actually managed to do this myself eventually. My version of it is:
binarystrip = cellfun(@(x) x(1:3), C, 'uniformoutput',false)
x = bin2dec(binarystrip)
Thanks for the input too.

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2022-1-25
More efficient than using CELLFUN:
C = {'0100101000101000';'0100101000101000';'0100101000101000';'0100101000101000'}
C = 4×1 cell array
{'0100101000101000'} {'0100101000101000'} {'0100101000101000'} {'0100101000101000'}
V = [4,2,1]*sscanf(sprintf('%s ',C{:}),'%1d%1d%1d%*d',[3,Inf])
V = 1×4
2 2 2 2

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by