-1

I have a cell array A, of dimension say 1x8 and each cell consist of 10x13xNmatirx(numerical values).

Example:

10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double 10x13x91 double

now i want to take mean and variance for 10x13(130 Values) across N. i.e, (1,1,1)(1,1,2)...(1,1,N). first 2 values indicates the points and the third one refers to the position.

When i try to apply mean over a 1x8 Cell array of the same dimension and cell values as mentioned above using the cellfun function, i get the following error.

A = 1x8 cell

B = cellfun(@mean,A)

Error using cellfun Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false.

i need the results like only 260 values(mean+variance) across 8 elements of 1x8 Cellarray,by the way i can ignore the N values, since i take the mean and variance over N. How can i do this? Thanks.

  • can you elaborate more clearly on what you want to do? You have 8 matrices with size 10x13x91. Do you want to have a mean value for every 10x13 matrix? That will give you a 1x91 long vector for every cell. Is that your goal? – Artyom Emelyanenko Dec 11 '17 at 13:21

3 Answers3

1

Instead of using @mean, use @(x)mean(x,3), and as the others have mentioned - ...,'UniformOutput',false.

Since the results of the computation always have the same size (10x13), you can convert the resulting cell to a numeric array, if you reshape B to be a vector in the 3rd dimension:

C = cell2mat(reshape(B,1,1,[]));

Now, if you also want to compute variance while you're at it, you can do something like

B = cellfun(@(x)cat(3,mean(x,3),var(x,0,3)),A,'UniformOutput',false);

But if you want this last B as a numeric array, you'd need to make it a vector in the 4th dimension (since the 3rd was taken by the concatenation):

C = cell2mat(reshape(B,1,1,1,[]));
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Thanks for the great help @Dev-iL,now i get a 10x13x2 double in each cell with `val(:,:,1)` as mean and `val(:,:,2)` as variance a total of 260 values,now my final doubt is how can i make this `10x13x2` double as a `260x1` or `1x260` matrix in each cell, such that it becomes like `(m1,v1) and (m2,v2) ...(Mn,Vn)` .(i.e) i want to concatenate these values of mean and variance together, Kindly Suggest, Thanks. – Deepak Eevil Personified Dec 12 '17 at 04:08
  • @DeepakEevilPersonified you should read the documentation of the `reshape` function, which I linked. To turn anything into a column vector you can use `out = reshape(in,[],1);`. – Dev-iL Dec 12 '17 at 11:01
0

The output from cellfun is not the same in every cell, so you need to write B = cellfun(@mean,A,'UniformOutput',false).

Richard
  • 256
  • 1
  • 7
0

I am not sure I understand the question. I think the code sequence you want is:

B = cellfun(@mean,A,'UniformOutput',false);
B = cellfun(@mean,B,'UniformOutput',false);
B = cellfun(@squeeze,B, 'UniformOutput', false);

the result here is:

B =

1×8 cell array

{91×1 double}    {91×1 double}    {91×1 double}    {91×1 double}    {91×1 double}    {91×1 double}    {91×1 double}    {91×1 double}

I am not sure what is the context of your data but assuming you want to find a single variance value for the whole 10x13 matrix you will need to flatten the matrix first before using function var.

for ii=8:-1:1
    flatA{ii} = reshape(A{ii}, 130, 91);
end

V = cellfun(@var, flatA, 'UniformOutput', false);
V = cellfun(@squeeze, V, 'UniformOutput', false);
Artyom Emelyanenko
  • 1,323
  • 1
  • 11
  • 16