Array values not corresponding to condition

조회 수: 1 (최근 30일)
Matlabhelp
Matlabhelp 2016년 9월 29일
댓글: Image Analyst 2020년 8월 25일
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
Matlabhelp
Matlabhelp 2016년 9월 29일
편집: Adam 2016년 9월 29일
Alright
mydata = dlmread('data-1.csv',','); ( 20x20 matrix )
roundData = round(mydata,2); ( just so you know where round data came from )
input Columns 1 through 6
1.9000 1.0300 1.5100 0.6400 0.2100 2.0000
0.1400 0.2300 1.6900 1.4500 1.7700 5.0000
1.7600 0.6500 1.9300 1.0700 0.5700 3.0000
3.0000 3.0000 3.0000 5.0000 4.0000 2.0000
Output Columns 1 through 6
5.0000 3.0000 4.0000 2.0000 3.0000 2.0000
3.0000 3.0000 5.0000 4.0000 5.0000 5.0000
5.0000 2.0000 5.0000 3.0000 2.0000 3.0000
3.0000 3.0000 3.0000 5.0000 4.0000 2.0000
Image Analyst
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
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
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.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by