creating cell array

1 次查看(过去 30 天)
Tor Fredrik Hove
Tor Fredrik Hove 2011-10-23
I would like to create a cell array with 1 row and two columns and in each column it is a columnvector of 5 elements in the first 5 numbers in the second 5 characters. This was my attempt:
>> cellarray2too1={5.3; 2.2; 3.3; 4.4; 1.1, 'a'; 'b'; 'a'; 'a'; 'b'} ??? Error using ==> vertcat CAT arguments dimensions are not consistent.

采纳的回答

Image Analyst
Image Analyst 2011-10-23
Look at your statement. After each semicolon it tries to make a new row in your cell array. So the first row has 5.3. The next row has 2.2. The next row has 3.3. The next row has 4.4. Each of those rows has one thing that goes into one cell. Now look at the next row. You have 1.1, 'a' and this is two things. It's trying to make that row be two cells when each of your prior rows was only one cell. It can't concatenate a two cell row to a columnar array of one-cell rows. That's why you got the error. You could either fix it like andrei suggested, or use this alternative:
cellarray2too1={5.3; 2.2; 3.3; 4.4; 1.1; 'a'; 'b'; 'a'; 'a'; 'b'}
depending on exactly what you want to have. Note I put "1.1; 'a'" so that it is now two rows of one cell each instead of one row of two cells. Think of a cell array as a grid of buckets on the floor. You can arrange the buckets into any rectangular array shape you want. And you can toss almost anything you want into each bucket. But your syntax tried to create a row of two buckets when all the other buckets were in a single line. You were trying to do this:
5.3
2.2
3.3
4.4
1.1, 'a'
'b'
'a'
'a'
'b'
and that is not allowed.

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2011-10-23
cellarray2too1={[5.3; 2.2; 3.3; 4.4; 1.1],[ 'a'; 'b'; 'a'; 'a'; 'b']}

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by