필터 지우기
필터 지우기

set some values to zero in a matrix

조회 수: 2 (최근 30일)
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 4월 13일
댓글: Nikolaos Zafirakis 2019년 4월 14일
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
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.
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 4월 14일
Hello well I actually took the code supplied by madhan below. The matrix is coordinates x-y-z and -x -y -z. I split the matrix into 3 sections making x and -x one matrix y and -y another ...etc. Then I applied the code by madhan to the 2x20000 matrix and took the largest value from this. Then I combined the matrix back together and have one value for each plane.

댓글을 달려면 로그인하십시오.

답변 (1개)

madhan ravi
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
  댓글 수: 1
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 4월 14일
Thanks that was very helpful! (See above i solved the issue).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by