How to loop in a single line for the given statement

39 views (last 30 days)
Suppose i have a n * d matrix (2D datas) . Here n=2 , so i have been able to give 2 statements inside for loop to calculate the mean normalization. But if n would have been higher, then the number of statements inside for loop would increase. Is there a way i can write it in a single line.
function meannormalize
F = [1 1;1 1.5;1.5 1.5;5 5;5 5.5;5.5 5;6 5;10 7;10.5 6;9 7;10 8]';
[n dim]=size(F);
MEAN=mean(F,2)
for i=1:dim
F(1,i)=(F(1,i)-MEAN(1))/norm(F(1,i)-MEAN(1));
F(2,i)=(F(2,i)-MEAN(2))/norm(F(2,i)-MEAN(2));
end
end
  6 Comments
Cam Salzberger
Cam Salzberger on 12 Oct 2017
If you are looking to normalize the data, you probably want norm(F-MEAN), as shown in my answer, as opposed to doing element-wise "normalization".
If you've got R2016a or before, you can do it with:
normVal = norm(F-repmat(MEAN, 1, size(F, 2)));
and then just divide that every loop, or in vectorized format.
OCDER
OCDER on 12 Oct 2017
Not familiar with k-means clustering, but are there 2 different "norm"? It seems you want to "Normalize" vectors in dimension D (row-wise) with respect to the "Euclidean norm" of that dimension's mean value, like this?
MEAN = mean(F, 2);
NORM = zeros(size(F));
for k = 1:size(F, 1)
NORM(k, :) = (F(k,:) - MEAN(k)) / norm(F(k,:) - MEAN(k));
end
NORM =
-0.4209 -0.4209 -0.3776 -0.0747 -0.0747 -0.0315 0.0118 0.3579 0.4012 0.2714 0.3579
-0.4946 -0.4291 -0.4291 0.0298 0.0953 0.0298 0.0298 0.2920 0.1609 0.2920 0.4231
Also, Matlab does have kmeans clustering function too, though not sure if it does what you want.

Sign in to comment.

Answers (1)

Cam Salzberger
Cam Salzberger on 12 Oct 2017
Edited: Cam Salzberger on 12 Oct 2017
The norm of a scalar is just going to be the absolute value, so that can be easily removed. Then you're doing basically (a - b)/abs(a - b). Which is just sign(a - b).
This works in R2016b+ due to implicit expansion:
sign(F-MEAN)
Otherwise, you can get fancy with repmat:
sign(F-repmat(MEAN, 1, size(F,2)))
For your future use, here's how you could do a double loop:
for jj = 1:dim
for ii = 1:n
F(ii, jj) = (F(ii, jj)-MEAN(ii))/norm(F(ii, jj)-MEAN(ii));
end
end
But it's easier to just vectorize the whole thing (R2016b+):
F = (F-MEAN)./norm(F-MEAN);
This will be a different result since the norm will actually be a scalar value (since it's taking the norm of the whole matrix. Not sure if that's what you really wanted. If not, you can take the "norm" of each scalar with just:
F = (F-MEAN)./abs(F-MEAN);
-Cam

Tags

Community Treasure Hunt

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

Start Hunting!