问题
I have a cell array VsCell with dimension 1000x1 (or more). Each item in VsCell is a 501x567 matrix. I want to extract elements occupying a position in all the matrices contained in the cell. Basically something like:
VsCell{:}(1,1) - for all the first elements of the arrays in the cell VsCell{:}(2,1) - for all the first row 2 elements of the arrays in the cell.
Subsequently, I intend to take the mean or median of these selection and fill a single [501x567] matrix array, which would represent the mean/median/etc of the VsCell arrays e.g. mean(VsCell{:}(1,1)).
I tried VsCell{:}(1,1) - but it returns "Bad cell reference operation."
Also, is there a way to achieve this with little or no for loops/cellfun? I couldn't really achieve this using examples found online.
Thank you very much for your time, I am glad to clarify further if need be.
回答1:
I don't think what you're trying to do can be done with cells, without loops or cellfun
, that is. Cells are heterogeneous data structures, there's no guarantee that each element will have a compatible shape. Heck, element number 3 in your cell could even be another cell, or a string, or a custom class instance! I'm just saying that I don't find it surprising that cells can't be indexed into in the way you're trying to.
However, you can concat
enate your cell into a higher-dimensional array, and work with that:
VsMat = cat(3,VsCell{:}); % cell as comma separated list
MatMeans = mean(VsMat,3); % mean along dimension 3, where they are concatenated
MatMedians = median(VsMat,3);
MatSums = sum(VsMat,3);
Working with arrays should be faster anyway, so except for the single trade-off of calling cat
(which is slow), you're probably better off using homogeneous arrays anyway.
来源:https://stackoverflow.com/questions/37557025/choosing-the-nth-element-in-a-matlab-cell-array