creating cell array
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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.
댓글 수: 0
추가 답변 (1개)
Andrei Bobrov
2011년 10월 23일
cellarray2too1={[5.3; 2.2; 3.3; 4.4; 1.1],[ 'a'; 'b'; 'a'; 'a'; 'b']}
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!