Why does C{1}(1) work but C(1){1} results in an invalid array index error in MATLAB?
10 次查看(过去 30 天)
显示 更早的评论
I'm trying to understand the difference between using curly braces {} and parentheses () when accessing elements inside a cell array in MATLAB. I noticed that C{1}(1) returns the first element of the first cell, but when I try C(1){1}, MATLAB throws an 'Invalid array index' error. Here's the code that leads to my confusion:
C = {[1,2],'abc'};
C{1}(1) % This works and ans = 1
C(1){1}
% This throws an error: Invalid array index. high-version MATLAB
% ()-indexing must appear last in an index expression. low-version MATLAB
I expected C(1){1} to also return the first element of the first cell, just like C{1}(1) does. Why does the second method result in an error? What's the correct way to access elements within a cell array using different types of braces?
Something extra:
By searching for keywords, I learned ()-indexing must appear last in an index expression.
I'm familiar with the concept of chained indexing from other programming languages, where a series of index operations can be performed in succession, like array[1][2].
I would like to understand the rationale behind MATLAB's requirement for parentheses indexing to appear last in an indexing expression. Is there a specific reason for this syntax rule in MATLAB, and how does it relate to the language's overall design for indexing operations?
2 个评论
Bruno Luong
2023-11-12
C(1){1}
"I expected C(1){1} to also return the first element of the first cell"
Actually no, if it would work it cascade
temp = C(1)
tem{1}
If you ran that you will get output
[1 2]
Stephen23
2023-11-12
编辑:Stephen23
2023-11-12
"I'm trying to understand the difference between using curly braces {} and parentheses () when accessing elements inside a cell array in MATLAB."
Both of them do NOT access "elements inside a cell array". The difference is very simple:
- {} curly braces refers to the cell array content, whereas
- () parentheses refer to the cell array itself.
If you want part of the cell array then use parentheses. If you want its content then use curly braces.
By the way, this concept is so simple. For every basic MATLAB data type, parentheses always return an array of the same type, i.e. refers to the array itself. And for container classes curly braces refer to the container content.
"I noticed that C{1}(1) returns the first element of the first cell"
No. It returns the first element of the content of the first cell. Always keep the difference clear in your mind.
"I'm familiar with the concept of chained indexing from other programming languages,"
Forget about chained indexing.
Some other languages use one notation to refer to the container (e.g. list) itself... and then obscure things somewhat by using exactly the same notation for its content. Ugh. They miss out on this simple clarity of curly braces and parentheses. Your example illustrates this:
"...where a series of index operations can be performed in succession, like array[1][2]."
is array[1] another array or is it the content of that array? What about array[1:3], is that another array or the array content? What is the sytnax for returning an array with one element (but not its content) ?
采纳的回答
dpb
2023-11-12
编辑:dpb
2023-11-13
The reason is the cell contains an array; you must first dereference the particular cell content and then index into the subsequent array.
The alternate sequence is indexing a cell first, not the content thereof. If it were to work, it would be the samething as C{1}, the content of the first element in the cell array.
"...chained indexing from other programming languages, where a series of index operations can be performed in succession, like array[1][2]."
Folks with other language programming experience (particularly those with C) often want to try to apply what they know about it to MATLAB, but "MATLAB is not C" (nor any other language, for that matter); you can't just expect things to be 1:1. In a C expression of the above, the quantity being indexed into must be all of the same type and a single element--that isn't at all necessarily the case with a MATLAB cell array in which the cell can be anything, including yet another cell...
MATLAB syntax is its own animal, "when in Rome..."
0 个评论
更多回答(2 个)
Paul
2023-11-12
C is 1x2 cell array
C = {[1,2],'abc'}
Curly brace indexing here "pops-out" the first element of C as an ordinary 1x2 vector
C{1}
Now we can pull out the first element usining paren indexing.
C{1}(1) % This works and ans = 1
Here, paren indexing pulls out the first cell, but the result is still a cell array.
C(1)
So if this were valid code
%C(1){1}
all it would be doing is to "pop-out" whatever is contained in the cell array C(1); it would not pop out the first element of whatever is contained in C(1). But that's just the same as C{1}.
0 个评论
Bruno Luong
2023-11-12
编辑:Bruno Luong
2023-11-12
"I would like to understand the rationale behind MATLAB's requirement for parentheses indexing to appear last in an indexing expression. Is there a specific reason for this syntax rule in MATLAB, and how does it relate to the language's overall design for indexing operations?"
I don't known exactly but it could be related that the syntax
C(1)
is first element of an array but in the same time calling function C with single argument 1.
Only TMW know the true reason, limitation of the parser.
To me syntaxically I don't see why the cascade indexing with parentheris or curly bracket cannot be used in single statement.
The answer is: "that's the way it is".
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!