Note that there are some explanatory texts on larger screens.

plurals
  1. POMatrix Transform
    text
    copied!<p>I am working on a project in which I need to transform the result matrix to a simpler matrix. More specifically, for each column in matrix, I want to find the maximum value in row and change it to 1, and the rest of the values will be change to 0. I am not sure how to search this question so I decide to post it here.</p> <p>For example, I have a matrix X:</p> <pre><code>X = [2 8 4; 7 3 9;1 2 3]; X = 2 8 4 7 3 9 1 2 3 </code></pre> <p>and I want to transform this matrix to </p> <pre><code>X' = 0 1 0 1 0 1 0 0 0 </code></pre> <p>I only know how to use for loop to return the maximum value from each row, but I am not sure how to return the position in matrix of that value and convert the rest to zero.</p> <p>Here is my idea</p> <ol> <li>Create a new same dimension zero matrix first</li> <li>Find the maximum value in row </li> <li>Find the position of the maximum value</li> <li>Assign the value 1 into new matrix at same position</li> <li>Repeat 2 and 3 by for loop</li> </ol> <p>I am new to Matlab, hope you can help.</p> <p>Here is one of the result of my program</p> <pre><code> 2.0680 17.7410 0.8992 -3.0221 3.6093 7.3443 6.7442 0.9874 -0.9095 -3.3220 -1.4857 -0.0023 -1.1753 -16.7906 0.3672 3.7697 -1.6856 -6.0929 -2.8614 0.5054 1.0794 3.8352 1.9894 0.1686 -0.4584 -0.3923 -1.2525 -0.4761 </code></pre> <p>And I want this matrix transform to </p> <pre><code>0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 </code></pre> <p>So I think the reason why max() not work is becuase I have negative value inside the matrix</p> <p>Other example which not wotk with max() Columns 1 through 5</p> <pre><code> 0.3816 23.2243 19.6435 23.2243 -3.1993 3.8674 8.0762 6.0563 8.0762 1.8475 -0.4442 -4.0758 -3.4244 -4.0758 0.2073 0.7639 -22.3618 -19.1365 -22.3618 3.9892 -1.8128 -6.4602 -5.1011 -6.4602 -0.4536 0.2954 3.5886 3.1529 3.5886 -0.1403 -0.3723 0.8057 0.6607 0.8057 -0.5173 </code></pre> <p>Columns 5 through 10</p> <pre><code> 1.4814 19.6435 3.3100 23.2243 1.4814 7.0255 6.0563 6.5251 8.0762 7.0255 -1.1483 -3.4244 -1.1448 -4.0758 -1.1483 -0.7784 -19.1365 -2.3904 -22.3618 -0.7784 -3.8880 -5.1011 -3.7437 -6.4602 -3.8880 1.0289 3.1529 0.7994 3.5886 1.0289 -0.3723 0.8057 0.6607 0.8057 -0.5173 </code></pre> <p>Columns 11 through 15</p> <pre><code> 21.3957 19.6435 1.4814 0.3816 -3.1993 8.5765 6.0563 7.0255 3.8674 1.8475 -4.0793 -3.4244 -1.1483 -0.4442 0.2073 -20.7498 -19.1365 -0.7784 0.7639 3.9892 -6.6046 -5.1011 -3.8880 -1.8128 -0.4536 3.8181 3.1529 1.0289 0.2954 -0.1403 0.4994 0.6607 -0.5829 -0.3723 -0.5173 </code></pre> <p>Columns 16 through 20</p> <pre><code> -1.7736 9.5635 20.0251 1.8072 0.3816 1.6114 7.0884 9.9237 3.6313 3.8674 -0.0058 -2.3835 -3.8685 -0.6572 -0.4442 2.1941 -8.7540 -18.3726 -1.0312 0.7639 -0.5815 -4.5430 -6.9138 -1.9407 -1.8128 0.1232 2.4470 3.4483 0.5589 0.2954 -0.3588 -0.2377 0.2884 -0.2138 -0.3723 </code></pre> <p>The correct answer is use bsxfun(@eq, output, max(output))</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload