Cell Array indexing and manipulating

2 ビュー (過去 30 日間)
Rick
Rick 2014 年 8 月 2 日
コメント済み: Image Analyst 2014 年 8 月 2 日
The following code is entered in the command window:
A = {'Cal', 'Golden', 'Bears', [5 7], {[1 2 3 4]}};
Enter a single-line command the extract the number 3 from A
A{5}{1}(3)
Hello, I am confused about this problem. Why wouldn't the answer be A{5}{3} ??
  1 件のコメント
Image Analyst
Image Analyst 2014 年 8 月 2 日
Have you read the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F It should give you an intuitive appreciation for cell arrays.

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 8 月 2 日
Rick - try the code in steps. Start with
A{5}
The above returns the fifth element of the cell array A as
ans =
[1x4 double]
But what is the data type and size? Try
class(A{5})
ans =
cell
size(A{5})
ans =
1 1
So it is a 1x1 cell array. So trying to use
A{5}{3}
will result in the Index exceeds matrix dimensions error as 3 is not a valid index into the 1x1 cell array. Now try
A{5}{1}
and determine its type and dimension as
class(A{5}{1})
ans =
double
size(A{5}{1})
ans =
1 4
It is a 1x4 array of doubles, and so we can access the 3 as
A{5}{1}(3)
Note in the above how we use the curly braces {} for cell arrays, and brackets () for non-cell arrays.

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2014 年 8 月 2 日
編集済み: Andrei Bobrov 2014 年 8 月 2 日
A = {'Cal', 'Golden', 'Bears', [5 7], {1 2 3 4}};
A{5}{3}
or
>> A = {'Cal', 'Golden', 'Bears', [5 7], [1 2 3 4]};
>> A{5}(3)
ans =
3
>>
  1 件のコメント
Rick
Rick 2014 年 8 月 2 日
編集済み: Rick 2014 年 8 月 2 日
You misunderstood the premise. I can't change what A is. I am supposed to extract 3 from A exactly the way it is. I was wondering why it is A{5}{1}(3)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by