Array values not corresponding to condition

1 Ansicht (letzte 30 Tage)
Matlabhelp
Matlabhelp am 29 Sep. 2016
Kommentiert: Image Analyst am 25 Aug. 2020
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 Kommentare
Matlabhelp
Matlabhelp am 29 Sep. 2016
Bearbeitet: Adam am 29 Sep. 2016
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 am 25 Aug. 2020
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;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Massimo Zanetti
Massimo Zanetti am 29 Sep. 2016
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.

Weitere Antworten (1)

Adam
Adam am 29 Sep. 2016
Bearbeitet: Adam am 29 Sep. 2016
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.

Community Treasure Hunt

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

Start Hunting!

Translated by