Converting cell of numbers to double

조회 수: 3 (최근 30일)
Andromeda
Andromeda 2022년 5월 27일
댓글: Steven Lord 2022년 5월 27일
Hi. How do I convert a cell of numbers from cell to double? I looked up the previously asked questions but they mostly involved strings and I have numbers. I didn't post any code, I just want to know if there's any way of doing it if you have a cell class with mutiple matrices in it. How can I also store a cell class in a structure array where each "f1" and "f2" will be the field?
This is what I am having
fie =
2×2 cell array
{["f1"]} {3×7 cell}
{["f2"]} {4×7 cell}
This is what I want
fie =
2×2 cell array
{["f1"]} {3×7 double}
{["f2"]} {4×7 double}

채택된 답변

Steven Lord
Steven Lord 2022년 5월 27일
If the cells in that cell array in the upper-right cell of your outer cell array can be concatenated to form a matrix you could use cell2mat.
C = mat2cell(reshape(1:21, 3, 7), ones(3, 1), [2, ones(1, 5)]) % Making sample data
C = 3×6 cell array
{[1 4]} {[7]} {[10]} {[13]} {[16]} {[19]} {[2 5]} {[8]} {[11]} {[14]} {[17]} {[20]} {[3 6]} {[9]} {[12]} {[15]} {[18]} {[21]}
A = {'abc', C}
A = 1×2 cell array
{'abc'} {3×6 cell}
B = cell2mat(A{1, 2})
B = 3×7
1 4 7 10 13 16 19 2 5 8 11 14 17 20 3 6 9 12 15 18 21
A{1, 2} = B
A = 1×2 cell array
{'abc'} {3×7 double}
This wouldn't work if C is not of a form that can be combined together into a matrix, like if the contents of the cells in C don't have compatible numbers of rows and/or columns.
D = {[1 2], 3; [4 5 6], 7} % Can't have a matrix whose rows have different numbers of columns
D = 2×2 cell array
{[ 1 2]} {[3]} {[4 5 6]} {[7]}
cell2mat(D)
Error using cat
Dimensions of arrays being concatenated are not consistent.

Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
  댓글 수: 4
Andromeda
Andromeda 2022년 5월 27일
편집: Andromeda 2022년 5월 27일
The question is related.
How do I store A in a structure array such that
structA = A : [3x7 double]
and
structA.A = 1 4 7 10 13 16 19
2 5 8 11 14 17 20
3 6 9 12 15 18 21
Steven Lord
Steven Lord 2022년 5월 27일
q = reshape(1:21, 3, 7)
q = 3×7
1 4 7 10 13 16 19 2 5 8 11 14 17 20 3 6 9 12 15 18 21
mystruct.A = q
mystruct = struct with fields:
A: [3×7 double]
y = mystruct.A
y = 3×7
1 4 7 10 13 16 19 2 5 8 11 14 17 20 3 6 9 12 15 18 21
check = isequal(q, y) % true
check = logical
1

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2022년 5월 27일
cell2mat()
cell2struct()

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by