choosing the nth element in a matlab cell array

落花浮王杯 提交于 2019-12-11 13:21:39

问题


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 concatenate 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!