Array values not corresponding to condition
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello
I have an array that has been which has been rounded to numbers of 1-5. ( i'll post the code below ). I have numbers which aren't corresponding to my given condition and instead take another condition, i was wondering if anyone can spot what i've done wrong.
" Round data is a 20 by 20 array of numbers rounded to numbers 1,2,3,4,5)
rangeValue1 = 1;
rangeValue2 = 2;
rangeValue3 = 3;
rangeValue4 = 4;
rangeValue5 = 5;
roundData (roundData < 0.4 & roundData > 0 ) = rangeValue1;
roundData (roundData < 0.8 & roundData > 0.4 ) = rangeValue2;
roundData (roundData < 1.2 & roundData > 0.8 ) = rangeValue3;
roundData (roundData < 1.6 & roundData > 1.2 ) = rangeValue4;
roundData (roundData < 2.0 & roundData > 1.6 ) = rangeValue5;
댓글 수: 3
Image Analyst
2020년 8월 25일
Original question in case he deletes it like he's done with other posts:
Hello
I have an array that has been which has been rounded to numbers of 1-5. ( i'll post the code below ). I have numbers which aren't corresponding to my given condition and instead take another condition, i was wondering if anyone can spot what i've done wrong.
" Round data is a 20 by 20 array of numbers rounded to numbers 1,2,3,4,5)
rangeValue1 = 1;
rangeValue2 = 2;
rangeValue3 = 3;
rangeValue4 = 4;
rangeValue5 = 5;
roundData (roundData < 0.4 & roundData > 0 ) = rangeValue1;
roundData (roundData < 0.8 & roundData > 0.4 ) = rangeValue2;
roundData (roundData < 1.2 & roundData > 0.8 ) = rangeValue3;
roundData (roundData < 1.6 & roundData > 1.2 ) = rangeValue4;
roundData (roundData < 2.0 & roundData > 1.6 ) = rangeValue5;
채택된 답변
Massimo Zanetti
2016년 9월 29일
Your problems only arise because you used only "<" operators, so the numbers that are exactly in the middle of your intervals do not change. You should include "<=" operators, so that you will not loose any value re-mapping:
roundData (roundData <= 0.4 & roundData > 0 ) = rangeValue1;
roundData (roundData <= 0.8 & roundData > 0.4 ) = rangeValue2;
roundData (roundData <= 1.2 & roundData > 0.8 ) = rangeValue3;
roundData (roundData <= 1.6 & roundData > 1.2 ) = rangeValue4;
roundData (roundData <= 2.0 & roundData > 1.6 ) = rangeValue5;
Sure this work.
추가 답변 (1개)
Adam
2016년 9월 29일
편집: Adam
2016년 9월 29일
You are doing your changes in-place and in sequence, so those that were caught by the 1st condition get changed to 1 and then also get caught by the 3rd condition and changed to 3.
Take a copy of your matrix and run the condition off the original matrix instead.
Massimo Zanetti's answer is also something I intended to point out but forget and does result in potential gaps in your output, though not in the case of the example you showed. This is a secondary point that will show up less often, but still needs fixing.
댓글 수: 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!