필터 지우기
필터 지우기

How can I create a cell array by combining arrays?

조회 수: 2 (최근 30일)
Billie Jean
Billie Jean 2016년 11월 12일
편집: Jan 2016년 11월 14일
Lets say;
names=['X1','X2','X3']
first=[1 2 3]
second=[4 5 6]
third=[7 8 9]
I need a cell array that is combination of these four arrays. It should be 1x4!
cellArray={['X','Y','Z'],[1 2 3],[4 5 6],[7 8 9]}
How can I write that code. horzcat is no good. Thanks!
  댓글 수: 1
Jan
Jan 2016년 11월 14일
편집: Jan 2016년 11월 14일
Note that
names=['X1','X2','X3']
is the same as
names='X1X2X3'
Therefore I cannot guess, what you want as output and how your input exactly should look like.

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

답변 (2개)

Ahmet Cecen
Ahmet Cecen 2016년 11월 12일
편집: Ahmet Cecen 2016년 11월 12일
maybe this? mat2cell
it is unclear what you are trying to accomplish with the cell conversion, so this is the best I can do.

George
George 2016년 11월 12일
What are you having trouble with? The code you posted works.
>> cellArray={['X','Y','Z'],[1 2 3],[4 5 6],[7 8 9]}
cellArray =
1×4 cell array
'XYZ' [1×3 double] [1×3 double] [1×3 double]
>>
  댓글 수: 3
Ahmet Cecen
Ahmet Cecen 2016년 11월 14일
편집: Ahmet Cecen 2016년 11월 14일
Oooh, string arrays are uncomfortable in MATLAB. Either do {'X','Y','Z'} or make a table() instead of a cell array.
['X','Y','Z'] is the concatenate constructor for strings, not an array.
Image Analyst
Image Analyst 2016년 11월 14일
Billie Jean, please read the FAQ on cell arrays. I think it will help you understand. http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
With ['X','Y','Z'] you are concetnating 3 single characters to form a 1-by-3 character array, also known as a string. They are the same thing. In fact you can also set it to 'XYZ' and get the same thing. Just look:
>> s=['X','Y','Z']
s =
XYZ
>> whos s
Name Size Bytes Class Attributes
s 1x3 6 char
>> s='XYZ'
s =
XYZ
>> whos s
Name Size Bytes Class Attributes
s 1x3 6 char
Now, that string is inside the first cell of a 1-by-4 cell array. A single cell, like the first one , is a 1-by-1 array if you want to look at it that way. But because it's a cell, it can contain virtually anything inside it. And in this case, that first cell contains a 1x3 string (character array) inside it. Think of a cell like a bucket, and a cell array as an array of buckets. You can throw anything you want into the buckets: a string, a double array, a structure, a table, even another cell or cell array. Different buckets can contain anything - you're not required to have a whole column or whole row have the same data type in it. You can vary the contents of the buckets on a bucket-by-bucket basis. Again, read the FAQ - you won't regret it.

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

카테고리

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