Gabriel Machado
Gabriel Machado

Reputation: 155

How to normalize matrix setting 0 for minimum values and 1 for maximum values?

I need to transform a neural network output matrix with size 2 X N in zeros and ones, where 0 will represent the minimum value of the column and 1 contrariwise. This will be necessary in order to calculate the confusion matrix.

For example, consider this matrix 2 X 8:

 2    33     4     5     6     7     8     9
 1    44     5     4     7     5     2     1

I need to get this result:

 1    0     0     1     0     1     1     1
 0    1     1     0     1     0     0     0

How can I do this in MATLAB without for loops? Thanks in advance.

Upvotes: 2

Views: 84

Answers (2)

J. Roles
J. Roles

Reputation: 78

If it's just 2xN, then this will work:

floor(A./[max(A); max(A)])

In general:

 floor(A./repmat(max(A),size(A,1),1))

Upvotes: 2

rcpinto
rcpinto

Reputation: 4298

>> d = [ 2    33     4     5     6     7     8     9;
         1    44     5     4     7     5     2     1];

>> bsxfun(@rdivide, bsxfun(@minus, d,  min(d)), max(d) - min(d))

ans =

     1     0     0     1     0     1     1     1
     0     1     1     0     1     0     0     0

The bsxfun function is necessary to broadcast the minus and division operations to matrices of different dimensions (min and max have only 1 row each).

Other solution is the following (works only for 2 rows):

>> [d(1,:) > d(2,:); d(1,:) < d(2,:)]

ans =

     1     0     0     1     0     1     1     1
     0     1     1     0     1     0     0     0

Upvotes: 4

Related Questions