How can I do the average between many vector columns?

42 ビュー (過去 30 日間)
Adrian
Adrian 2014 年 4 月 16 日
コメント済み: Umesh Gautam 2023 年 11 月 7 日
I have 120 1x500 vector columns, so I can't do it manually. For example, I have the following vector columns, each 1x500 in size:
a1, a2, ......, an, where n=120
I want to do:
(a1 + a2 + ... + an)/n
Thank you very much!
Adrian

採用された回答

José-Luis
José-Luis 2014 年 4 月 16 日
編集済み: José-Luis 2014 年 4 月 16 日
%Creating vectors:
for ii = 1:50
str = ['a' num2str(ii)];
assignin('base',str,rand(100,1));
end
%Preallocating:
your_mat = ones(100,50);
%Putting everything together, this is the part you want if you already have the vectors
for ii = 1:50
str = ['a' num2str(ii)];
your_mat(:,ii) = evalin('base',str);
end
your_val = mean(your_mat(:));
Please accept an answer if it helps you.
  2 件のコメント
Adrian
Adrian 2014 年 4 月 17 日
Thank you for your answer. But the thing is that it gives me just a number, because that's what 'mean' function does, it gives a single number. When I average my 120 500x1 column vectors, namely (a1 + a2 + ... + an)/n, where n=120, it should give me a 500x1 vector column, not a single number.
If you could help further, that would be great! Thanks
lvn
lvn 2014 年 4 月 17 日
Replace the last line of Jose-Luis code with
your_val = mean(your_mat);
Then you will have what you want. However, much better would be to initially read in/or store your data in a matrix, rather than in separate vectors, then you can simply use what Azzi wrote.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 16 日
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
You can use
mean(A,2)
Umesh Gautam
Umesh Gautam 2023 年 11 月 7 日
@Azzi Abdelmalek Thanks...this is a simple solution and it works well.

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

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by