Manipulation of cell to convert into a matrix

2 ビュー (過去 30 日間)
Guillaume
Guillaume 2022 年 10 月 26 日
コメント済み: Guillaume 2022 年 10 月 27 日
Hello everyone,
Assume that A = 2x2 cell, with :
A = {[5] [1,1,0] ;[4] [1,1]}
A = 2×2 cell array
{[5]} {[1 1 0]} {[4]} {[ 1 1]}
I'm trying to convert it into a matrix array. Is there a way to transform A to this matrix A_convert :
A_convert = [5 110 ; 4 10]
A_convert = 2×2
5 110 4 10
Because the cells {1,2} and {2,2} are supposed to be binary code.
Or Is it possible to have some matrix with mixed classes ? For example, we can imagine have :
[5 '110' ; 4 '10']
I already try functions cell2 'smth' but it dosen't work the way I want to do my conversion, it not the answer i'm looking for.
To illustrate my point, for example, I cannot run the script below because of a dimensions of arry issue :
% A_try = cell2mat(A)
Because, with this script we're trying to do that : [5 1 1 0 ; 4 1 0] and of course the dimensions don't match.
Thanks in advance for your help !
  2 件のコメント
Jan
Jan 2022 年 10 月 26 日
Converting [1,1,0] to 110 does not look like a binary code. This is a decimal conversion.
[5 '110' ; 4 '10'] is not possible in Matlab. To mix variables of different type in one array, cell arrays are the correct method. So what about using the original cell? What do you consider as benefit of [5 '110' ; 4 '10']?
Guillaume
Guillaume 2022 年 10 月 26 日
Binary is just the physical meaning of the data. So, is it possible to do this decimal conversion : [1,1,0] to 110 ?
My purpose is to reduce the size of those elements. The original cell is 472 Bytes.
A = {[5] [1,1,0] ;[4] [1,1]} ; whos A
Name Size Bytes Class Attributes A 2x2 472 cell
Whereas the other format would be much interesting, only 32 Bytes :
A_convert = [5 110 ; 4 10]; whos A_convert
Name Size Bytes Class Attributes A_convert 2x2 32 double

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

採用された回答

Matt J
Matt J 2022 年 10 月 26 日
編集済み: Matt J 2022 年 10 月 26 日
Perhaps this is what you want?
A = {[5] [1,1,0] ;[4] [0,0]}
A = 2×2 cell array
{[5]} {[1 1 0]} {[4]} {[ 0 0]}
A_convert = cellfun(@(c) join(string(c),''), A)
A_convert = 2×2 string array
"5" "110" "4" "00"
Or perhaps as follows?
A_convert = A;
A(:,2) = cellfun(@(c) char(join(string(c),'')), A(:,2),'uni',0)
A = 2×2 cell array
{[5]} {'110'} {[4]} {'00' }
  3 件のコメント
Matt J
Matt J 2022 年 10 月 27 日
This solution can match, thank you !
You're quite welcome, but please Accept-click the answer to indicate so.
But I don't understand the meaning of "@(c)" notably the "@" use, can you explain ?
The @ operator is used to create different types of function handles, see,
Guillaume
Guillaume 2022 年 10 月 27 日
Many thanks, I really think that your proposal can work on my project. I will work on it to fit it with my data !
I'll also check on youre link to learn about function handles.

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

その他の回答 (0 件)

カテゴリ

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