필터 지우기
필터 지우기

Accessing data in cell arrays

조회 수: 1 (최근 30일)
DrKarimKecir
DrKarimKecir 2015년 2월 16일
댓글: DrKarimKecir 2015년 2월 17일
Hi,
I have a basic question to which I can't find an answer.
Suppose we have a cell matrix:
C = cell(2,2);
What is the difference between
C{1,2} = 2;
and
C{1}{2} = 2;
?
Thanks in advance,
KK

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 2월 16일
편집: Geoff Hayes 2015년 2월 16일
Karim - what do you observe if you try the above two statements? The first
C{1,2} = 2;
initializes the cell array in the first row and second column to 2. So if
>> C = cell(2,2)
C =
[] []
[] []
then
>> C{1,2} = 2
C =
[] [2]
[] []
As for the second statement, it initializes the first cell array element of C to be a cell array of two elements with the second set to 2. So
>> C{1}{2} = 2
C =
{1x2 cell} [2]
[] []
where
>> C{1}
ans =
[] [2]
Remember that arrays (cell or otherwise) can be accessed using the row column pair (as in your first example) or using a linear index (as in your second example). For more details on linear indexing see http://www.mathworks.com/help/matlab/math/matrix-indexing.html#f1-85511.
  댓글 수: 3
Stephen23
Stephen23 2015년 2월 16일
Great answer, I think it only needs a mention of that they also use different indexing styles:
C{1,2} % one instance of subscript indexing (row,col)
C{1}{2} % two examples of linear indexing (element #)
Adam
Adam 2015년 2월 16일
Ah, I was too late :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2015년 2월 16일
편집: Adam 2015년 2월 16일
Simplest way to learn these things is to try it on the command line and see what the result is.
C = cell(2,2)
C =
[] []
[] []
>> C{1,2} = 2
C =
[] [2]
[] []
C2 = cell(2,2)
C2 =
[] []
[] []
>> C2{1}{2} = 2
C2 =
{1x2 cell} []
[] []
The first syntax is the standard one which does exactly what you expect, i.e. it puts the value 2 into the cell indexed at row 1, column 2. Thus:
C{1,2}
will return the value 2 as a double and
C(1,2)
would return a 1*1 cell array containing the single value 2.
The second syntax is one I never use as it is rather less intuitive and not useful for me. It will index into the first cell of the cell array using 1d indexing and then further use 1d indexing to create a cell array of 1x2, the second element of which is the 2 that you specify.
  댓글 수: 8
Stephen23
Stephen23 2015년 2월 17일
편집: Stephen23 2015년 2월 17일
To get the best performance out of MATLAB you want to not keep resizing your variables. For example this code enlarges the vector on every iteration:
A = [];
for k = 1:1e4;
A(k) = sin(k);
end
Whereas iterating in reverse will create the whole vector on the first iteration, after which it does not change size again:
A = [];
for k = 1e4:-1:1
A(k) = sin(k);
end
Alternatively you can preallocate the vector and iterate in the usual direction:
A = nan(1,1e4);
for k = 1:1e4
A(k) = sin(k);
end
Have a play with tic and toc, you might be surprised how much faster well written code can be in MATLAB.
DrKarimKecir
DrKarimKecir 2015년 2월 17일
Yes, that's exactly what I said : " not to change the size of…".
Thank you for the tricks, Stephen, and have a nice day!
KK

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by