필터 지우기
필터 지우기

How to normalize values in a matrix to be between 0 and 1?

조회 수: 108 (최근 30일)
Sahar abdalah
Sahar abdalah 2015년 4월 8일
댓글: Tubi 2018년 3월 22일
I have a matrix Ypred that contain negative values and I want to normalize this matrix between 0 and 1
Ypred=[-0.9630 -1.0107 -1.0774
-1.2075 -1.4164 -1.2135
-1.0237 -1.0082 -1.0714
-1.0191 -1.3686 -1.2105];
I'm new in matlab, please help me, there is a matlab function or toolbox that can do this? thanks
  댓글 수: 2
James Tursa
James Tursa 2015년 4월 8일
편집: James Tursa 2015년 4월 8일
What do you mean by "normalize"? Divide by the max value in the matrix and make all values positive? Do this by columns or rows? Divide by norm of columns or rows? Or what?
Sahar abdalah
Sahar abdalah 2015년 4월 8일
I want to divide by norm of row to make positive values

댓글을 달려면 로그인하십시오.

채택된 답변

Jos (10584)
Jos (10584) 2015년 4월 8일
편집: Jos (10584) 2015년 4월 8일
This can be simply done in a two step process
  1. subtract the minimum
  2. divide by the new maximum
normA = A - min(A(:))
normA = normA ./ max(normA(:)) % *
note that A(:) makes A into a long list of values. Otherwise min(A) would not return a single value ... Try fro yourself!
  • Edited after comment ...
  댓글 수: 2
Sahar abdalah
Sahar abdalah 2015년 4월 8일
Thanks for the code, I just tried this and it not normalised between 0 and 1
Ypred =
-0.9630 -1.0107 -1.0774
-1.2075 -1.4164 -1.2135
-1.0237 -1.0082 -1.0714
-1.0191 -1.3686 -1.2105
normA =
0.4534 0.4057 0.3390
0.2089 0 0.2029
0.3927 0.4082 0.3450
0.3973 0.0478 0.2059
normA =
1.0000 1.0495 1.1188
1.2539 1.4708 1.2601
1.0630 1.0469 1.1126
1.0583 1.4212 1.2570
Jos (10584)
Jos (10584) 2015년 4월 8일
Sorry! The second line of code is wrong ;-) It should read
normA = normA ./ max(normA(:))

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

James Tursa
James Tursa 2015년 4월 8일
NormRows = sqrt(sum(Ypred.*Ypred,2));
Ynorm = bsxfun(@rdivide,abs(Ypred),NormRows);
  댓글 수: 3
John D'Errico
John D'Errico 2018년 3월 10일
편집: John D'Errico 2018년 3월 10일
Why would you need a citation? If you truly want a citation, just cite MATLAB itself, or perhaps the doc page for bsxfun, but it seems a bit silly to need a citation for a simple code fragment.
Tubi
Tubi 2018년 3월 22일
Many Thanks John, I believe you are right in your suggestion since they are mostly common MATLAB commands and functions.

댓글을 달려면 로그인하십시오.


Sahar abdalah
Sahar abdalah 2015년 4월 9일
thank you for your answers. I used both codes and I found two different result. what is the result that I can use?
normA = Ypred - min(Ypred(:))
normA = normA ./ max(normA(:))
normA =
1.0000 0.8948 0.7477
0.4607 0 0.4475
0.8661 0.9003 0.7609
0.8763 0.1054 0.4541
NormRows = sqrt(sum(Ypred.*Ypred,2));
Ynorm = bsxfun(@rdivide,abs(Ypred),NormRows);
Ynorm =
0.5461 0.5731 0.6110
0.5435 0.6375 0.5462
0.5712 0.5625 0.5978
0.4871 0.6542 0.5786
  댓글 수: 2
James Tursa
James Tursa 2015년 4월 9일
Use whichever is appropriate for your problem. Jos and I are both trying to interpret what you want, but without any background about the problem you are solving it is a bit of a guessing game at our end.

댓글을 달려면 로그인하십시오.


c4ndc
c4ndc 2017년 8월 12일
Hello, What is the name of this norm in the accepted answer? (Euclidean, Frobenius etc.)
  댓글 수: 1
Jan
Jan 2017년 8월 12일
Please post comments to answers in the section for the comments. You message is not an answer.
The accepted answer does not contain a norm at all, but a "normalization". A matrix norm would reply a scalar, the normalization replies a matrix with the same size, but with shifted and scaled values.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by