Converint between Cell arrays and Numbers and Strings

5 ビュー (過去 30 日間)
Kash022
Kash022 2019 年 1 月 25 日
編集済み: Stephen23 2019 年 1 月 25 日
Hello,
I am trying to convert a double cell array as shown to individual 8 bit cells. (I hope I have been able to frame the question correctly, if not please take a look at my screenshot)
I want to convert each contents to individual bits, like for e.g [10000111] to individual 1 0 0 0 0 1 1 1
I have tried everything from cell2num, mat2cell, cell arrays and all but keep getting errors.
Please help me in solving this; also a little background theory on what these types of cells and 'cellarrays' mean would be a great help for next time!
Thanks,
Cheers,
kash022
  2 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 25 日
Result=cellfun(@(x) x-'0',string(yourcellarray),'un',0)
Stephen23
Stephen23 2019 年 1 月 25 日
編集済み: Stephen23 2019 年 1 月 25 日
"Converint between Cell arrays and Numbers and Strings"
"I am trying to convert a double cell array as shown..."
Nothing in your screenshot is related to cell arrays:
Nothing in your question is related to string arrays:
After using MATLAB for more than two years it would be a good idea to learn what the basic data classes are and how to identify them:
"also a little background theory on what these types of cells and 'cellarrays' mean would be a great help for next time!"
Why not try reading the MATLAB documentation?:
PS: this is not twitter. Please do not put ugly # symbols at the start of each tag.

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

採用された回答

Jan
Jan 2019 年 1 月 25 日
編集済み: Jan 2019 年 1 月 25 日
This obtains the digits of a decimal number:
x = 10000111;
n = floor(log10(x));
out = rem(floor(x(:) ./ power(10, n:-1:0)), 10)
>> [1 0 0 0 0 1 1 1]
I prefer this instead of the indirection of letting sprintf convert the number to a char vector and converting it back to a number.
"Cell arrays" are arrays of the class cell. These are array, which can contain elements of different sizes and classes. See:
doc cell
Example:
C = {[], 1, 1:2, 1:3, 'And a char vector also'}
If you want to store the output in one array and do not want leading zeros, you need a cell array, because the vectors have dirrent length.
x = [1, 101, 10000111];
C = cell(size(x));
nMax = floor(log10(max(x)));
P = power(10, nMax:-1:0); % Expensive power operation once only
for k = 1:numel(x)
n = floor(log10(x(k)));
C{k} = rem(floor(x(k) ./ P(nMax-n+1:end)), 10);
end
Note: You are working with Matlab since at least March 2016. You are expected to be able to read the documentation of the cell command.
  3 件のコメント
Kash022
Kash022 2019 年 1 月 25 日
編集済み: Kash022 2019 年 1 月 25 日
@Jan what happens if I need leading zeros?
Jan
Jan 2019 年 1 月 25 日
Leading zeros allow a simpler code:
x = 10000111;
n = floor(log10(max(x))); % Here the maximum value matters
out = rem(floor(x(:) ./ power(10, n:-1:0)), 10)

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

その他の回答 (2 件)

per isakson
per isakson 2019 年 1 月 25 日
編集済み: per isakson 2019 年 1 月 25 日
"cell array as shown" The screenshot doesn't show any cell array
Another approach
>> str = sprintf('%d', 10000111 );
>> num = reshape( sscanf( str, '%1d' ), 1,[] )
num =
1 0 0 0 0 1 1 1
>>
  4 件のコメント
Jan
Jan 2019 年 1 月 25 日
Exploiting the old-fashioned ASCII coding is less magic than calling sprintf, which is a huge library function. In very old Matlab versions, sprintf suffered from a bug, which allowed to gain admin privileges only by using strange format strings.
per isakson
per isakson 2019 年 1 月 25 日
>> '€'+'ab' % undocumented behaviour(?)
ans =
8461 8462
>> "€"+"ab" % documented behaviour
ans =
"€ab"

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


Walter Roberson
Walter Roberson 2019 年 1 月 25 日
I do not think you have a cell array at present. I think you have a double array.
OutputCell = arrayfun(@(V) sprintf('%08d', V) - '0'), YourArrayNameGoesHere, 'uniform', 0)
  2 件のコメント
Kash022
Kash022 2019 年 1 月 25 日
sorry..this doesn't work..it outputs [1,0,1,0,1,0,0,1] which is not what I want...
it should be like [1] [0] [1] [0] [1] [0] [0] [1]
Kash022
Kash022 2019 年 1 月 25 日
well, it works like this now
my_cell = cell2mat(OutputCell);

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by