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
2013년 4월 1일
k = 0;
while (reading file) ...
k = k+1;
match(k,:) = strcmp('Hello', out)
end
M = mean(match)
댓글 수: 10
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.
Dont use variablename k for the counter/index if k is allready in use.
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?
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)
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.
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?
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>
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
You're welcome.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
