Array values not corresponding to condition
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Matlabhelp
el 29 de Sept. de 2016
Comentada: Image Analyst
el 25 de Ag. de 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 comentarios
Image Analyst
el 25 de Ag. de 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;
Respuesta aceptada
Massimo Zanetti
el 29 de Sept. de 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.
Más respuestas (1)
Adam
el 29 de Sept. de 2016
Editada: Adam
el 29 de Sept. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!