Get the max (and its index) of a row in MATLAB

Given a M x N matrix, getting a column vector of the max of each row and another with the index of such maximum is done as follows:

h = sigmoid(a3);
[p, i] = max(h, [], 2);

p -> column vector of the maximum values.
i -> column vector of the maximum value indexes.

Advertisements

Assigning a vector to a matrix row in MATLAB

I spent one hour trying to figure out an error in MATLAB, I’ve learned something useful.

I had:

for c = 1:num_labels
initial_theta = zeros(n + 1, 1);
[all_theta(c)] = (fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
initial_theta, options))'
end

It was:

for c = 1:num_labels
initial_theta = zeros(n + 1, 1);
[all_theta(c,:)] = (fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
initial_theta, options))'
end

Explanation:

[all_theta(c)] is a scalar

[all_theta(c,:)] is a vector

Difference between “one-liner” and “script” comunication

http://www.amazon.com/Masterminds-Programming-Conversations-Creators-Languages-ebook/dp/B0043D2EEU/ref=sr_1_1?ie=UTF8&qid=1392418392&sr=8-1&keywords=masterminds+of+programming

Answered by the Perl language creator. I have been thinking about this question since a long time ago, many similar answers came up from my mind. As far as it concerns to me, this guys explain it very, very well. This post is really super-awesome.

One liners