Why is MyCellArray{:} different than x = MyCellArray{:}?

1 view (last 30 days)
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{:}?

Accepted Answer

Zachary
Zachary on 1 Sep 2015
Edited: Zachary on 1 Sep 2015
Thank you for your responses but never mind this question. I feel sort of ridiculous about this, but my code for calculating the mean matrix was wrong. The ans that resulted from MyCellArray{:} was simply the last matrix, and x = MyCellArray{:} results in the first matrix.
For those of you who wish to calculate the mean of each matrix element across the 32 cells, assuming the size of each cell is the same (600x1 in this case) without using loops, I figured out the following code:
meanArray = arrayfun(@(y) mean(cellfun(@(x) x(y), MyCellArray)), [1:600])'
  1 Comment
Cedric
Cedric on 1 Sep 2015
Edited: Cedric on 1 Sep 2015
Then what about the following:
meanArray = mean(cat(3, MyCellArray{:}), 3) ;
It could be 100s of times faster than a solution mixing ARRAYFUN and CELLFUN.

Sign in to comment.

More Answers (3)

Cedric
Cedric on 1 Sep 2015
Edited: Cedric on 1 Sep 2015
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
James Tursa on 1 Sep 2015
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).

Azzi Abdelmalek
Azzi Abdelmalek on 1 Sep 2015
a={rand(600,1) ;rand(600,1)}
b=cellfun(@mean,a)
  1 Comment
Zachary
Zachary on 1 Sep 2015
This averages each array, i was hoping for an average of each element across the 32 arrays. See below.

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!