更新: 这就是我要完成的工作...除了这只是将整个矩阵中的最大值更改为1。我想将每个COLUMN中的最大值更改为1。
one_hots = bsxfun(@eq, mini_batch_activations, max(mini_batch_activations(:)))
更新2: 这是我想要的,但仅适用于行。我需要专栏。
V = max(mini_batch_activations,[],2);
idx = mini_batch_activations == V;
迭代代码:
% This is the matrix I want to one hot encode
mini_batch_activations = activations{length(layers)};
%For each vector in the mini_batch:
for m = 1:size(mini_batch_activations, 2)
% Isolate column vector for mini_batch
vector = mini_batch_activations(:,m);
% One hot encode vector to compare to target vector
one_hot = zeros(size(mini_batch_activations, 1),1);
[max_val,ind] = max(vector);
one_hot(ind) = 1;
% Isolate corresponding column vector in targets
mini_batch = mini_batch_y{k};
target_vector = mini_batch(:,m);
% Compare one_hot to target vector , and increment result if they match
if isequal(one_hot, target_vector)
num_correct = num_correct + 1;
endif
...
endfor
答案 0 :(得分:1)
您已获得每一列的最大值:
V = max(mini_batch_activations,[],1); % note 1, not 2!
现在您需要做的就是相等比较,输出是一个逻辑数组,可以很容易地转换为0和1。请注意,MATLAB和Octave进行隐式单例扩展:
one_hot = mini_batch_activations==V;