Calculate average of multiple matrices by reading an m.file
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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.
Accepted Answer
ChristianW
on 1 Apr 2013
k = 0;
while (reading file) ...
k = k+1;
match(k,:) = strcmp('Hello', out)
end
M = mean(match)
10 Comments
Samira
on 1 Apr 2013
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
on 1 Apr 2013
Dont use variablename k for the counter/index if k is allready in use.
Samira
on 2 Apr 2013
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
on 2 Apr 2013
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
on 3 Apr 2013
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
on 3 Apr 2013
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?
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
on 3 Apr 2013
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
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
on 3 Apr 2013
You're welcome.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)