How to Change element values of an array with conditions?
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
satheeshkumar satheeshkumar M
am 31 Okt. 2017
Kommentiert: Fangjun Jiang
am 11 Nov. 2019
If suppose, A=[2 3 4 7 9 10] i want to use if condition to add (e.g) 10 to the elements less than 6 (< 6) of array A, so that my array should convert into A=[12 13 14 7 9 10]. how can i achieve?
0 Kommentare
Akzeptierte Antwort
Adam
am 31 Okt. 2017
Bearbeitet: Adam
am 31 Okt. 2017
A( A < 6 ) = A ( A < 6 ) + 10
Sadly Matlab doesn't have a neat += operator so you have to do the above rather ugly equivalent, though I would often pull the logical indexing out into its own variable.
3 Kommentare
Erik Elander Aman
am 10 Nov. 2019
Is there a similar solution for when you would like to alter in an interval within two limits, eg. adding 10 to each value between 4 and 6 in the array?
Weitere Antworten (1)
Fangjun Jiang
am 31 Okt. 2017
Play golf?
A=A+10*(A<6)
2 Kommentare
Eliot Bethke
am 20 Mär. 2019
I prefer this answer because you can assign the result of the operation to a different variable. Also works if you wanted to multiply instead of add:
S = A .* 10 .* (A < 6);
Siehe auch
Kategorien
Mehr zu Simulink Environment Customization finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!