Why is MyCellArray{:} different than x = MyCellArray{:}?
이전 댓글 표시
I have a 32x1 cell array, each cell is a 600x1 matrix. I originally wanted to average the matrix elements across the 32 cells, the result being a 600x1 mean matrix. Purely by accident I discovered that
MyCellArray{:}
results in the desired mean matrix, while
x = MyCellArray{:}
results in the contents of the first array. I would like to know why they are different, and how can I automatically assign the ans from MyCellArray{:}?
채택된 답변
추가 답변 (3개)
MyCellArray{:} is a Comma Separated List (CSL). It is the information that you are missing I guess. The x on the left hand side (LHS) is another one with one element. MATLAB assigns values to elements of the LHS in the order they appear in the RHS.
EDIT: to illustrate
Define a cell array with basic rand arrays rand, 10+rand, 20+rand (that you can recognize):
>> C = {rand(2), 10+rand(2), 20+rand(2)} ;
Full CSL:
>> C{:}
ans =
0.3404 0.2238
0.5853 0.7513
ans =
10.2551 10.6991
10.5060 10.8909
ans =
20.9593 20.1386
20.5472 20.1493
Assign first element to single element LHS
>> x = C{:}
x =
0.3404 0.2238
0.5853 0.7513
Assign first two elements to two elements of the LHS:
>> [x,y] = C{:}
x =
0.3404 0.2238
0.5853 0.7513
y =
10.2551 10.6991
10.5060 10.8909
Assign first three elements to three elements of LHS:
>> [x,y,z] = C{:}
x =
0.3404 0.2238
0.5853 0.7513
y =
10.2551 10.6991
10.5060 10.8909
z =
20.9593 20.1386
20.5472 20.1493
James Tursa
2015년 9월 1일
MyCellArray{:} is a comma separated list. It is equivalent to typing the following:
MyCellArray{1},MyCellArray{2},MyCellArray{3},...,MyCellArray{32} % <-- typing all 32 elements
x = MyCellArray{:} is an assignment with a comma separated list on the right hand side. E.g.,
x = MyCellArray{1},MyCellArray{2},MyCellArray{3},...,MyCellArray{32} % <-- typing all 32 elements
So in the second case, you basically have 32 different expressions that you have asked MATLAB to evaluate. The first expression assigns MyCellArray{1} to x, and the rest simply return the remaining 32 elements (and promptly get disgarded).
카테고리
도움말 센터 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!