0

I have a matrix and a row vector produced by the std function:

X = [1 2 3; 4 5 6];
sigma = std(X);

Now I would like a vectorized solution that updates each value in X by dividing the value with the correct value in sigma. It would look something like this:

X(1,1)/sigma(1)
X(1,2)/sigma(2)
X(1,3)/sigma(3)
X(2,1)/sigma(1)
X(2,2)/sigma(2)
X(2,3)/sigma(3)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user3139545
  • 6,882
  • 13
  • 44
  • 87

2 Answers2

2

Just expand sigma and use elementwise division

Y = X ./ repmat (sigma, rows (X), 1)
Y =

   0.47140   0.94281   1.41421
   1.88562   2.35702   2.82843
Andy
  • 7,931
  • 4
  • 25
  • 45
0

A more elegant solution would be to use bsxfun, which is faster and cleaner than repmat. The topic has been extensively covered here.

> bsxfun(@rdivide,X,sigma)
ans =

   0.47140   0.94281   1.41421
   1.88562   2.35702   2.82843
Community
  • 1
  • 1
wizclown
  • 313
  • 1
  • 4
  • 16