フィルターのクリア

How to write a for loop to obtain z-scores

2 ビュー (過去 30 日間)
mcm
mcm 2016 年 11 月 2 日
コメント済み: Star Strider 2016 年 11 月 2 日
Although matlab has a built in z-score function, I need to create a for loop that would do the same thing. How do I improve my code
load('data1');
data1_mean = mean(data1); data1_std = std(data1);
[r,c] = size(data1);
for i = 1:r %Data1 is 50x1 so we need the loop to evaluate for all 50 rows x = (i - data1_mean ./ data1_std; end disp(x)

回答 (1 件)

Star Strider
Star Strider 2016 年 11 月 2 日
You likely can do away with the loop entirely and just use vectorised code:
data1_mean = mean(data1);
data1_std = std(data1);
x = (data1 - data1_mean)./data1_std;
  2 件のコメント
mcm
mcm 2016 年 11 月 2 日
I am aware of that, but what if I want to use a for loop to achieve the same result?
Star Strider
Star Strider 2016 年 11 月 2 日
That’s straightforward:
for k1 = 1:length(data1)
x(k1) = (data1(k1) - data1_mean)./data1_std;
end
That’s how I would do it.
NOTE This is UNTESTED CODE but should work.

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

カテゴリ

Help Center および File ExchangeHypothesis Tests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by