Calculate average of multiple matrices by reading an m.file

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.
For example while(reading file) ...
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.

 採用された回答

ChristianW
ChristianW 2013 年 4 月 1 日

0 投票

k = 0;
while (reading file) ...
k = k+1;
match(k,:) = strcmp('Hello', out)
end
M = mean(match)

10 件のコメント

Samira
Samira 2013 年 4 月 1 日
I just tried your code, and it's giving me this error, and doesn't read past k=k+1;
??? Subscript indices must either be real positive integers or logicals.
But match is logical, so I don't understand why this error comes up.
ChristianW
ChristianW 2013 年 4 月 1 日
Dont use variablename k for the counter/index if k is allready in use.
Samira
Samira 2013 年 4 月 2 日
Thank you, I've kinda got it to work. It seem I have the wrong dimensions. My match is now a 2 by 5 logical array. When I'm testing this out, I'm using a script with 3 lines. But it doesn't read past the second line. This error comes : ??? Subscripted assignment dimension mismatch.
Obviously, I have the wrong dimensions, how do I fix this error?
ChristianW
ChristianW 2013 年 4 月 2 日
Explain the diversity of your (read) file or the out-variable.
files = {{'Hello','bla','bla'}
{'bla','Hello','Hello'}
{'Hello','Hello','bla'}};
for k = 1:length(files)
out = files{k};
match(k,:) = strcmp('Hello', out);
end
M = mean(match)
Samira
Samira 2013 年 4 月 3 日
This is my code so far
i=0
fid=fopen('test.m')
while~ feof(fid)
line=fgetl(fid);
if isempty(line)||strncmp(line, '%',1)||~ischar(line)
continue
end
fprintf(line);
out=regexp(line, ' ', 'split')
i=i+1
match(i,:)=strcmp('Hello', out)
end
M=mean(match)
match is a 2 by 5 logical cell and out is a 1 by 8 array. This one is just an example though, my actual code is reading a file with 100's of conversations between two people so it is a big file.
ChristianW
ChristianW 2013 年 4 月 3 日
Your statements are full of contradictions.
Are the number for words per line allways the same? Because this ...
files = {{'Hello','bla','bla'}
{'bla','Hello','Hello'}
{'Hello','Hello','bla','Hello'}}; % input for my code above
... can not work the way you planned it. How do you want to deal with that input? Do you want a logical array like this:
match = [ 1 0 0 NaN
0 1 1 NaN
1 1 0 1 ]
or like this:
match = [ 1 0 0 0
0 1 1 0
1 1 0 1 ]
or something else?
Samira
Samira 2013 年 4 月 3 日
編集済み: Samira 2013 年 4 月 3 日
My file has sentences of different length, so the number of words differ. I want it to be as the later.
files = {{'Hello','bla','bla'} {'bla','Hello','Hello'} {'Hello','Hello','bla','Hello'}}; I'm created a matrix for each sentence so my output is like this
match=[1 0 0 0]
match=[0 1 1 0]
match=[1 1 0 1]
I want it this way so I can work out the average. Is the reason I'm having difficulty because of the difference in the number of words per matrix?
Would it work if I made all matrices the same length, in this case n=4?
ChristianW
ChristianW 2013 年 4 月 3 日
No, that is not your output. Your output is like this:
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 2013 年 4 月 3 日
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 2013 年 4 月 3 日
You're welcome.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by