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!