Replacing the Maximum Element per Row of a Matrix with a Different Element
Show older comments
Hello everyone, I am trying to replace the maximum entry of every row in a matrix with a -1, using MatLab. However, the replacement only occurs in the first row of the matrix, so far this is my code:
m = 240;
n = 5;
for i = 1:m
for j = 1:n
if (num_matrix(i,j) == max(num_matrix,[],2))
num_matrix(i,j) = -1;
end
end
end
Thanks for your help.
Accepted Answer
More Answers (2)
Azzi Abdelmalek
on 14 Aug 2016
A=randi(100,4,5)
m=max(A,[],2)
A(bsxfun(@eq,A,m))=-1
dpb
on 14 Aug 2016
>> x=randi(20,4)
x =
15 19 15 14
6 9 14 4
9 20 11 3
11 7 14 20
>> [~,ix]=max(x,[],2);
>> x(sub2ind(size(x),[1:4].',ix))=-1
x =
15 -1 15 14
6 9 -1 4
9 -1 11 3
11 7 14 -1
>>
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!