## Calculate average of multiple matrices by reading an m.file

on 1 Apr 2013

### ChristianW (view profile)

I'm having difficulty in calculating the mean of multiple matrices. I know how to calculate the mean, I have tried to use mean(match,3) in 3 dimension , but it does not give me the mean of all my matrices by columns If I calculate my mean in the while loop, it creates a mean for every matrix, and if I do it outside the while loop, it does it only for the last matrix.

if(some conditions) continue

end

match= strcmp('Hello', out)//compares script with the word hello and outputs a logical array of 1s and 0s

M=mean(match,3)

end

match=[1 0 0 0 0 0] match=[0 0 1 0 0 0] match=[0 0 1 0 0 0]

I want the mean to be M=[1/3 0 2/3 0 0 0]

Do I need to use a for loop? Because it seems to be a much longer process to use a for loop to get the mean, but I do not see how else I could do this.

## Products

No products are associated with this question.

### ChristianW (view profile)

on 1 Apr 2013

```k = 0;
k = k+1;
match(k,:) = strcmp('Hello', out)
end
M = mean(match)
```

ChristianW

### ChristianW (view profile)

on 3 Apr 2013

```match=[1 0 0]
match=[0 1 1]
match=[1 1 0 1]
```

And yes, thats very possible the reason for your difficulties. To fill the empty space with zeros, you could use:

```ml = strcmp('Hello', out); % match for the line
match(i,1:length(ml)) = ml; %#ok<SAGROW>
```
Samira

### Samira (view profile)

on 3 Apr 2013

Sorry, I meant this is what I want my output to be like. Thank you so much, it works perfectly now! I have been trying to get this code to work for some time, thanks for taking the time to help me out, much appreciated. :D

ChristianW

### ChristianW (view profile)

on 3 Apr 2013

You're welcome.

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Apply Today

New to MATLAB?

Learn MATLAB today!