Using for loops in order to find average of matrix

5 ビュー (過去 30 日間)
Catayoun Lissa Eleonore Azarm
Catayoun Lissa Eleonore Azarm 2021 年 2 月 1 日
コメント済み: Bob Thompson 2021 年 2 月 1 日
Hi I have a 1000x100000 matrix.
I'd like to know the average of the numbers in each column, so that's 100000 values I'm looking for.
I can use the mean() command, and I'm trying to get this done using for loop and preallocation. What i have atm:
mu = 2;
SD = 3;
m = 1000;
n = 100000;
random_matrix = randn(m,n)*SD + mu;
a=zeros(1,n);
for k=1:m
for h=1:n
a(k,h) = means(random_matrix(k,h));
end
end
I dont know whats wrong arrgh. Can someone help me? I think i am not wrong at all, but my program does not stop running...
Thanks in advance!

採用された回答

Bob Thompson
Bob Thompson 2021 年 2 月 1 日
You can use mean to average all values in a certain direction.
mu = 2;
SD = 3;
m = 1000;
n = 100000;
random_matrix = randn(m,n)*SD + mu;
a=mean(random_matrix,1);
  5 件のコメント
Catayoun Lissa Eleonore Azarm
Catayoun Lissa Eleonore Azarm 2021 年 2 月 1 日
It worked for every case exept this one:
zscored1=zeros(m,n);
tic;
for i = 1:n
zscored1(i) = (random_matrix(:,i) - means1) ./ SDs1;
end
x(3)=toc;
It says "Unable to perform assignment because the left and right sides have a different number of elements." Do you know whats wrong?
Bob Thompson
Bob Thompson 2021 年 2 月 1 日
It means that the size of random_matrix(:,i), means1, and SDs1 don't agree. Either means1 and SDs1 need to be single elements or they need to be the same size as random_matrix(:,i).

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by