set some values to zero in a matrix
4 次查看(过去 30 天)
显示 更早的评论
Hello, I have a 6x20000 matrix. I would like make the 3 smalest values on each row equal to zero. For example if I have:
[ 1, 1, 1, 1.5, 1.5, 1.5 = [ 0, 0, 0, 1.5, 1.5, 1.5
1, 1.5, 1, 1, 1.5, 1.5 0, 1.5, 0, 0, 1.5, 1.5
1, 1.5, 1, 1, 1.5, 1.5] 0, 1.5, 0, 0, 1.5, 1.5]
Does anyone know how to do this?
2 个评论
Walter Roberson
2019-4-14
What do you want to do if there are four or more copies of the same minimum value?
What do you want to do if there is a distinct minumum value, but then there is a tie for second or third place such that the total number of values no greater than the third smallest, is more than 3? Such as [1 1.5 1 1.5 1.5 1.5]: here the two 1's are smallest, but there is a tie of 1.5's for third.
回答(1 个)
madhan ravi
2019-4-14
编辑:madhan ravi
2019-4-14
Note: Have the same doubt as sir Walter but see if the below answer does what you want.
[~,I]=mink(a,3,2); % requires 2017 b and later if prior sort each row and pick first three values in each row, a is your matrix
idx=sub2ind(size(a), repmat((1:size(a,1)).',1,size(I,2)) ,I);
a(idx)=0
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!