Finding a range in an array

조회 수: 157 (최근 30일)
Rafay Ali
Rafay Ali 2019년 5월 27일
편집: madhan ravi 2019년 5월 27일
I have an array of 8000 values. I want to find a range of particular values say for example the values which are between 20 - 25.6 should be multiplied by -1 but the values should be in changed in the same array. I am currently using a the find nested in an if but somehow I cannot store it in the same array.
  댓글 수: 1
Rafay Ali
Rafay Ali 2019년 5월 27일
In one of my searches, I came across this. Will it be possible for me to make it
r(r>2) = r * -1;
% Generate random data between -3 and 3
a = -3;
b = 3;
r = (b-a).*rand(100,1) + a;
r(r>2) = 2 ; % Change numbers greater then 2
r(r<0) = 0 ;% Change numbers less then 0

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

채택된 답변

madhan ravi
madhan ravi 2019년 5월 27일
편집: madhan ravi 2019년 5월 27일
idx = (array>=20) & (array<=25.6);
array(idx) = -array(idx)
  댓글 수: 2
Rafay Ali
Rafay Ali 2019년 5월 27일
Thanks and is it possible that instead of these particular values I go particular index values? For example, make all the values between 2222 to 2245 be multiplied by a negative number.
2019_05_27_12_39_08_MATLAB_R2017b.jpg
madhan ravi
madhan ravi 2019년 5월 27일
편집: madhan ravi 2019년 5월 27일
I suggest you to do MATLAB onramp course to cover the basics of MATLAB.
idx = (array>=20) & (array<=25.6); % using this as indices is already much more efficient than find()
indices = find(idx) % this would give you the indices where the condition satisfies
array(2222 : 2245) = - some_number * array(2222 : 2245)

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 5월 27일
Let A - your array.
Anew = A.*(1 - 2*(A >= 20 & A <= 26.5));

카테고리

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