Why does C{1}(1) work but C(1){1} results in an invalid array index error in MATLAB?

조회 수: 4 (최근 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
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
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
dpb 2023년 11월 12일
편집: dpb 2023년 11월 13일
If you haven't yet, read <Access Data in Cell Array>.
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..."

추가 답변 (2개)

Paul
Paul 2023년 11월 12일
C is 1x2 cell array
C = {[1,2],'abc'}
C = 1×2 cell array
{[1 2]} {'abc'}
Curly brace indexing here "pops-out" the first element of C as an ordinary 1x2 vector
C{1}
ans = 1×2
1 2
Now we can pull out the first element usining paren indexing.
C{1}(1) % This works and ans = 1
ans = 1
Here, paren indexing pulls out the first cell, but the result is still a cell array.
C(1)
ans = 1×1 cell array
{[1 2]}
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}.

Bruno Luong
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".

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by