I'm trying to create a classes matrix in MATLAB preferably without using for loops. So I have A
:
A = [ 1 2 3 4;
2 5 4 1;
7 2 3 4];
Now I want to create a classes matrix so I want to look for the max value of each column and whatever that position is will become a "1". So using A
, I want B
to look like this:
B = [ 0 0 0 1;
0 1 0 0;
1 0 0 0];
I've been trying to use the following code to create the B
matrix and then search for the max value in each column using the following code:
[rows_A columns_A] = size(A);
B = zeros(rows_A, columns_A);
max(A, [], 2);
Then I'm trying to use ind2sub
to get the position within A
so I can put a "1" in the B
matrix. This approach isn't working out. The matrix A
can be of any dimensions. Any help is greatly appreciated.