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.
採用された回答
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
