Scaling the natrix in specific range

I have a 300x1 matrix
where the minmum value is
-2.1188e+003
maximum is
460.3171
i want to scale in region from -1 to 1

回答 (2 件)

Nasser M. Abbasi
Nasser M. Abbasi 2012 年 12 月 1 日

2 投票

Since I just wrote the code, might as well post it. A quick function to scale it
function C = nma_rescale(A,new_min,new_max)
%Nasser M. Abbasi 011212
%NO ERROR CHECKING DONE ON INPUT. Rescale a matrix or a vector A
current_max = max(A(:));
current_min = min(A(:));
C =((A-current_min)*(new_max-new_min))/(current_max-current_min) + new_min;
end
to use:
EDU>> C=rand(3);
EDU>> B=nma_rescale(C,-1,1)
B =
-0.7625 0.8960 -0.2033
-0.3188 -0.8938 -1.0000
-0.8317 1.0000 -0.3316

6 件のコメント

kash
kash 2012 年 12 月 1 日
I have some values of 300x1 matrix A= -2.1188e+003 to 460.3171 and other values B= -1 to 1 ,i want to scale A in range betweem -1 to 1 corresponding to values in B,please help
Nasser M. Abbasi
Nasser M. Abbasi 2012 年 12 月 1 日
I am not sure I understand. To scale a matrix A to be between -1 and 1, just type
C = nma_rescale(A,-1,1)
I do not understand what B role is here. May be you can update your question and make it more clear what is needed
Walter Roberson
Walter Roberson 2012 年 12 月 1 日
The code in my Answer does that, at least when given vectors (the problems statement involved vectors.) But before you go further you should decide whether the negative range is to be scaled independently of the positive range (as in my first solution), or if the entire range is to be treated as a linear block to be re-centered at the average of the max and min (as in my second solution.)
Dario Dematties
Dario Dematties 2017 年 4 月 18 日
Hello, with respect to the solution proposed by Nasser M. Abbasi. What should I do when current_max=current_min? What is the conventional way to solve this problem?
Jan
Jan 2017 年 4 月 18 日
@Dario: There is no convention. It depends on your problem, if the result of a scaled scalar is -1, 0, +1 or Inf, perhaps NaN.
Dario Dematties
Dario Dematties 2017 年 4 月 18 日
Thanks Jan, Does anyone know which is the reaction of libsvm software on matlab when it is fed with vectors with missing data (NaN)?

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

Walter Roberson
Walter Roberson 2012 年 12 月 1 日
編集済み: Walter Roberson 2012 年 12 月 1 日

0 投票

Does 0 need to remain 0, or should it become the midpoint of your [-2118.8 to 460.317] range?
0 remains 0:
matmin = min(mat);
matmax = max(mat);
newmat = mat;
newmat(mat < 0) = mat(mat < 0) ./ abs(minmat);
newmat(mat > 0) = mat(mat > 0) ./ maxmat;
Midpoint becomes 0:
matmin = min(mat);
matmax = max(mat);
newmat = 2 * (mat - matmin) ./ (matmax - matmin) - 1;

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

2012 年 12 月 1 日

編集済み:

2017 年 4 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by