Effacer les filtres
Effacer les filtres

Normalize unit vector to single didgit integers

4 vues (au cours des 30 derniers jours)
Jacob Weiss
Jacob Weiss le 15 Sep 2021
Modifié(e) : Stephen23 le 16 Sep 2021
I am trying to normalize a vector to integers. For example, I would like [ .4 .-4 .8] to be [ 1 -1 2]. However, if it is not a perfect for example, [.123 .123 .5] would be [123 123 500] but I would like it to be rounded to a single didgit integer. I.E instead of [123 123 500] it would be [1 1 5].
Ive seen v=x/norm(x) but this does not work even for obvious cases.

Réponses (2)

the cyclist
the cyclist le 15 Sep 2021
Does the following do what you want?
f = @(t) round(t/abs(min(t)));
f([ 0.4 -0.4 0.8])
ans = 1×3
1 -1 2
f([ 0.123 0.123 .5])
ans = 1×3
1 1 4
It gives a different result for your second example, but I think it is a better normalization because it is closer to the original ratio.

Stephen23
Stephen23 le 16 Sep 2021
Modifié(e) : Stephen23 le 16 Sep 2021
fun = @(V) round(V./10.^floor(log10(abs(V))));
fun([0.4,-0.4,0.8])
ans = 1×3
4 -4 8
fun([0.123,0.123,0.5])
ans = 1×3
1 1 5
Note that this scales each element independently, to give the result you asked for. The ratio of values is better represented by the cyclist's answer.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by