I want to convert cell 'A' to a matrix 'B'.
A=
columns 1 through 10
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}...
columns 1 through 20
{1x1 cell} {1x1 cell} {1x1 cell} {1x1 cell}...
I want to have a 'B' as:
B =
1 2 3 4 5 6 7 8 9 ... 20
I tried B(1)=cell2mat(A{1}), but can I avoid doing it one by one?
Thanks!

댓글 수: 1

Jan
Jan 2012년 7월 20일
As usual it would be helpful if you post the input data in valid Matlab syntax, such that we can try our suggestion by copy&paste them. Currently it is not clear what the "{1x1 cell}" contains. Looking at your comments it seems, like these are cell strings.

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2012년 7월 19일

8 개 추천

B = [A{:}];
B = [B{:}];

댓글 수: 4

Ken
Ken 2012년 7월 19일
Thanks Sean, this works for me.
:)
Ken
Ken 2012년 7월 19일
편집: Ken 2012년 7월 19일
Sean, instead of getting B= 1 2 3 4 ... 20
I have B=12345678910...20. (one number)
any help?
Thanks again!
Are the contents strings?
If so, throw in a:
B = cellfun(@str2double,B);
B = [B{:}]
Ken
Ken 2012년 7월 19일
Thanks!!

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

추가 답변 (1개)

Greg Heath
Greg Heath 2012년 7월 20일

0 개 추천

The expressions converting a "row cell of cells" to the corresponding "row vector" are
B = [ A{ : } ]
C = [ B{ : } ]
The expression for converting a 2-D cell of cells to the corresponding 2-D matrix is
C = repmat(cell2mat( [ A{:} ] ),size(A))

댓글 수: 5

Jan
Jan 2012년 7월 20일
I think this should be RESHAPE instead of REPMAT.
Anthony Hudetz
Anthony Hudetz 2019년 1월 9일
Does not work for me. I tried this and get an error:
a = {1:5,2:10,3:4,4:6,5:9}
C = reshape(cell2mat( [ a{:} ] ),size(a))
Brace indexing is not supported for variables of this
type.
Error in cell2mat (line 42)
cellclass = class(c{1});
Stephen23
Stephen23 2019년 1월 9일
편집: Stephen23 2019년 1월 9일
@Anthony Hudetz: note that Greg Heath wrote "The expression for converting a 2-D cell of cells..." Do you have a cell array of cell arrays, as Greg Heath wrote? No, you have a cell array of numeric arrays:
a = {1:5,2:10,3:4,4:6,5:9}
Perhaps this does what you want:
>> v = [a{:}]
v =
1 2 3 4 5 2 3 4 5 6 7 8 9 10 3 4 4 5 6 5 6 7 8 9
You can learn more about how this works here:
Rachel Clark
Rachel Clark 2021년 1월 12일
is there a way to make this work for a cell array containing cell arrays of different sizes?
Dijle Kaya
Dijle Kaya 2021년 3월 30일
편집: Dijle Kaya 2021년 3월 30일
Maybe so:
a = strings(1,length(A)) % or a = zeros(1,length(A)), if A is an integerarray
for i = 1:length(A)
a(1,i) = (convertCharsToStrings(A{i})) %if A is a Stringarray
end

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

Ken
2012년 7월 19일

편집:

2021년 3월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by